40 lines
1.1 KiB
Go
40 lines
1.1 KiB
Go
package controller
|
||
|
||
import (
|
||
"context"
|
||
|
||
"cidservice/model/dto"
|
||
"cidservice/service"
|
||
)
|
||
|
||
var (
|
||
RateLimit = rateLimit{}
|
||
)
|
||
|
||
type rateLimit struct{}
|
||
|
||
// SetTenantRateLimit 设置租户限流配置
|
||
func (c *rateLimit) SetTenantRateLimit(ctx context.Context, req *dto.SetTenantRateLimitReq) (res *dto.SetTenantRateLimitRes, err error) {
|
||
// 注意:实际使用的是config.yml中的全局配置,此接口仅用于兼容旧API
|
||
// 实际限流参数请修改config.yml中的tenantRateLimit部分
|
||
|
||
return &dto.SetTenantRateLimitRes{
|
||
Success: true,
|
||
}, nil
|
||
}
|
||
|
||
// GetTenantRateLimitUsage 获取租户限流使用情况
|
||
func (c *rateLimit) GetTenantRateLimitUsage(ctx context.Context, req *dto.GetTenantRateLimitUsageReq) (res *dto.GetTenantRateLimitUsageRes, err error) {
|
||
current, max, err := service.RateLimit.GetTenantCurrentUsage(ctx, req.TenantID, nil)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
return &dto.GetTenantRateLimitUsageRes{
|
||
TenantID: req.TenantID,
|
||
CurrentUsed: current,
|
||
MaxAllowed: max,
|
||
UsagePercent: float64(current) / float64(max) * 100,
|
||
}, nil
|
||
}
|