From ab445203aadd9f865b482ff74ce57757e942d48b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sun, 4 Jan 2026 12:49:56 +0800 Subject: [PATCH] feat: code cleanup --- abstract/limit.go | 5 +-- define/limit.go | 4 -- memory.go | 9 ++--- redis.go | 99 ++--------------------------------------------- 4 files changed, 7 insertions(+), 110 deletions(-) diff --git a/abstract/limit.go b/abstract/limit.go index 0dd3382..59dc658 100644 --- a/abstract/limit.go +++ b/abstract/limit.go @@ -9,14 +9,11 @@ package abstract import ( "context" + "git.zhangdeman.cn/zhangdeman/rate_limit/define" ) // IRateLimit 流控实现接口约束 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 11:33 2024/6/20 type IRateLimit interface { AllowN(ctx context.Context, limitCfg *define.LimitConfig, tokenCnt int) (bool, error) // 申请N个令牌, N >= 0 Reset(ctx context.Context, limitCfg *define.LimitConfig) error // 重置 diff --git a/define/limit.go b/define/limit.go index 7e9ad56..a5d4d31 100644 --- a/define/limit.go +++ b/define/limit.go @@ -8,10 +8,6 @@ package define // LimitConfig ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 15:17 2024/6/20 type LimitConfig struct { Key string `json:"key"` // 操作的key Total int `json:"total"` // 令牌桶大小 diff --git a/memory.go b/memory.go index c61dc1c..cbfe05c 100644 --- a/memory.go +++ b/memory.go @@ -9,10 +9,11 @@ package rate_limit import ( "context" - "git.zhangdeman.cn/zhangdeman/rate_limit/define" - "golang.org/x/time/rate" "sync" "time" + + "git.zhangdeman.cn/zhangdeman/rate_limit/define" + "golang.org/x/time/rate" ) var ( @@ -23,10 +24,6 @@ var ( ) // Memory ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 20:40 2024/6/20 type Memory struct { lock *sync.RWMutex limiterTable map[string]*rate.Limiter diff --git a/redis.go b/redis.go index d12a2c7..0d78982 100644 --- a/redis.go +++ b/redis.go @@ -9,11 +9,12 @@ package rate_limit import ( "context" + "sync" + "time" + "git.zhangdeman.cn/zhangdeman/rate_limit/define" "github.com/go-redis/redis_rate/v10" redisInstance "github.com/redis/go-redis/v9" - "sync" - "time" ) var ( @@ -54,97 +55,3 @@ func (r *Redis) AllowN(ctx context.Context, limitCfg *define.LimitConfig, tokenC func (r *Redis) Reset(ctx context.Context, limitCfg *define.LimitConfig) error { return r.limiter.Reset(ctx, limitCfg.Key) } - -/*func (r *Redis) Second(ctx context.Context, key string, total, rate int) (bool, error) { - if total <= 0 || rate <= 0 { - return true, nil - } - res, err := r.limiter.AllowN(ctx, key, redis_rate.PerSecond(total), rate) - if nil != err { - return false, err - } - return res.Allowed > 0, nil -} - -func (r *Redis) Minute(ctx context.Context, key string, total, rate int) (bool, error) { - if total <= 0 || rate <= 0 { - return true, nil - } - res, err := r.limiter.AllowN(ctx, key, redis_rate.PerMinute(total), rate) - if nil != err { - return false, err - } - return res.Allowed > 0, nil -} - -func (r *Redis) Hour(ctx context.Context, key string, total, rate int) (bool, error) { - if total <= 0 || rate <= 0 { - return true, nil - } - res, err := r.limiter.AllowN(ctx, key, redis_rate.PerHour(total), rate) - if nil != err { - return false, err - } - return res.Allowed > 0, nil -} - -func (r *Redis) Day(ctx context.Context, key string, total, rate int) (bool, error) { - if total <= 0 || rate <= 0 { - return true, nil - } - res, err := r.limiter.AllowN(ctx, key, redis_rate.Limit{ - Rate: rate, - Period: 24 * time.Hour, - Burst: total, - }, rate) - if nil != err { - return false, err - } - return res.Allowed > 0, nil -} - -func (r *Redis) Month(ctx context.Context, key string, total, rate int) (bool, error) { - if total <= 0 || rate <= 0 { - return true, nil - } - res, err := r.limiter.AllowN(ctx, key, redis_rate.Limit{ - Rate: rate, - Period: 24 * 30 * time.Hour, - Burst: total, - }, rate) - if nil != err { - return false, err - } - return res.Allowed > 0, nil -} - -func (r *Redis) Year(ctx context.Context, key string, total, rate int) (bool, error) { - if total <= 0 || rate <= 0 { - return true, nil - } - res, err := r.limiter.AllowN(ctx, key, redis_rate.Limit{ - Rate: rate, - Period: 24 * 30 * 365 * time.Hour, - Burst: total, - }, rate) - if nil != err { - return false, err - } - return res.Allowed > 0, nil -} - -func (r *Redis) Custom(ctx context.Context, timeInfo int64, key string, total, rate int) (bool, error) { - if total <= 0 || rate <= 0 { - return true, nil - } - res, err := r.limiter.AllowN(ctx, key, redis_rate.Limit{ - Rate: rate, - Period: time.Duration(timeInfo) * time.Second, - Burst: total, - }, rate) - if nil != err { - return false, err - } - return res.Allowed > 0, nil -} -*/