2025-12-06 15:24:30 +08:00
|
|
|
|
package controller
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
|
2025-12-09 16:10:45 +08:00
|
|
|
|
"cid/model/dto"
|
|
|
|
|
|
"cid/service"
|
2025-12-06 15:24:30 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2025-12-08 16:56:38 +08:00
|
|
|
|
var RateLimit = new(rateLimit)
|
2025-12-06 15:24:30 +08:00
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
}
|