优化mongo,封装count逻辑,处理objectId
This commit is contained in:
@@ -10,11 +10,10 @@ import (
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
)
|
||||
|
||||
var (
|
||||
RateLimit = rateLimitService{}
|
||||
)
|
||||
type rateLimit struct{}
|
||||
|
||||
type rateLimitService struct{}
|
||||
// RateLimit 限流服务
|
||||
var RateLimit = new(rateLimit)
|
||||
|
||||
// TenantRateLimitConfig 租户限流配置
|
||||
type TenantRateLimitConfig struct {
|
||||
@@ -25,7 +24,7 @@ type TenantRateLimitConfig struct {
|
||||
}
|
||||
|
||||
// CheckTenantRequestLimit 检查租户请求次数限制
|
||||
func (s *rateLimitService) CheckTenantRequestLimit(ctx context.Context, tenantID int64, config *TenantRateLimitConfig) (bool, error) {
|
||||
func (s *rateLimit) CheckTenantRequestLimit(ctx context.Context, tenantID int64, config *TenantRateLimitConfig) (bool, error) {
|
||||
if config == nil {
|
||||
// 使用默认配置
|
||||
config = s.getDefaultTenantRateLimitConfig(tenantID)
|
||||
@@ -72,7 +71,7 @@ func (s *rateLimitService) CheckTenantRequestLimit(ctx context.Context, tenantID
|
||||
}
|
||||
|
||||
// GetDefaultTenantRateLimitConfig 获取默认的租户限流配置
|
||||
func (s *rateLimitService) getDefaultTenantRateLimitConfig(tenantID int64) *TenantRateLimitConfig {
|
||||
func (s *rateLimit) getDefaultTenantRateLimitConfig(tenantID int64) *TenantRateLimitConfig {
|
||||
// 从配置文件中读取限流参数
|
||||
ctx := context.Background()
|
||||
|
||||
@@ -105,14 +104,14 @@ func (s *rateLimitService) getDefaultTenantRateLimitConfig(tenantID int64) *Tena
|
||||
}
|
||||
|
||||
// SetTenantRateLimitConfig 设置租户限流配置
|
||||
func (s *rateLimitService) SetTenantRateLimitConfig(ctx context.Context, config *TenantRateLimitConfig) error {
|
||||
func (s *rateLimit) SetTenantRateLimitConfig(ctx context.Context, config *TenantRateLimitConfig) error {
|
||||
// 注意:实际使用的是config.yml中的全局配置,此方法仅用于兼容旧API
|
||||
// 实际限流参数请修改config.yml中的tenantRateLimit部分
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetTenantCurrentUsage 获取租户当前请求使用情况
|
||||
func (s *rateLimitService) GetTenantCurrentUsage(ctx context.Context, tenantID int64, config *TenantRateLimitConfig) (current int64, max int64, err error) {
|
||||
func (s *rateLimit) GetTenantCurrentUsage(ctx context.Context, tenantID int64, config *TenantRateLimitConfig) (current int64, max int64, err error) {
|
||||
if config == nil {
|
||||
config = s.getDefaultTenantRateLimitConfig(tenantID)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user