Files
cid/controller/rate_limit_controller.go
2025-12-06 15:24:30 +08:00

40 lines
1.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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
}