diff --git a/abstract/limit.go b/abstract/limit.go index dc6ffef..df0f8f3 100644 --- a/abstract/limit.go +++ b/abstract/limit.go @@ -26,7 +26,7 @@ type IRateLimit interface { // Month(ctx context.Context, key string, total, rate int) (bool, error) // 每月访问次数限制 // Year(ctx context.Context, key string, total, rate int) (bool, error) // 每年访问次数限制 // Custom(ctx context.Context, timeInfo int64, key string, total, rate int) (bool, error) // 自定义指定时间内的访问次数, timeInfo 单位 : s - SetRedisClient(client *redis.ClusterClient) // 设置redis客户端 + SetRedisClient(client *redis.Client) // 设置redis客户端 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/go.mod b/go.mod index 55645bb..d0d7c33 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module git.zhangdeman.cn/zhangdeman/rate_limit go 1.22.4 require ( - git.zhangdeman.cn/zhangdeman/redis v0.0.0-20240620102217-f11c0a8216f6 + git.zhangdeman.cn/zhangdeman/redis v0.0.0-20240620103823-e8b63fb86391 github.com/go-redis/redis_rate/v10 v10.0.1 github.com/redis/go-redis/v9 v9.5.3 ) diff --git a/go.sum b/go.sum index acfdf50..6c3cd3e 100644 --- a/go.sum +++ b/go.sum @@ -10,6 +10,8 @@ git.zhangdeman.cn/zhangdeman/redis v0.0.0-20240620100925-63e4412d67e1 h1:sI8ON6x git.zhangdeman.cn/zhangdeman/redis v0.0.0-20240620100925-63e4412d67e1/go.mod h1:OImVgoNTw9qWxGrgrym0wtEMdx0ekaH7vJJKIgSpdEg= git.zhangdeman.cn/zhangdeman/redis v0.0.0-20240620102217-f11c0a8216f6 h1:2q6yC/vBhDOLLjqziqMJUSuF3bgA3wFDC/swFMOrTPU= git.zhangdeman.cn/zhangdeman/redis v0.0.0-20240620102217-f11c0a8216f6/go.mod h1:OImVgoNTw9qWxGrgrym0wtEMdx0ekaH7vJJKIgSpdEg= +git.zhangdeman.cn/zhangdeman/redis v0.0.0-20240620103823-e8b63fb86391 h1:DLzEEPHoFPqQlowIU6VPT/45FVnmVG6lVoeI/VHZ+Ho= +git.zhangdeman.cn/zhangdeman/redis v0.0.0-20240620103823-e8b63fb86391/go.mod h1:OImVgoNTw9qWxGrgrym0wtEMdx0ekaH7vJJKIgSpdEg= git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240325080031-1f58204e8687 h1:uQcGqdzi4UdpZlp4f4FUPeBqoygP58pEKJkmN3ROsE0= git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240325080031-1f58204e8687/go.mod h1:gf7SW2TXATgux8pfdFedMkXWv2515OtIIM/5c4atkFw= git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240618035451-8d48a6bd39dd h1:2Y37waOVCmVvx0Rp8VGEptE2/2JVMImtxB4dKKDk/3w= diff --git a/redis.go b/redis.go index 038fab3..d12a2c7 100644 --- a/redis.go +++ b/redis.go @@ -26,12 +26,12 @@ var ( // // Date : 11:44 2024/6/20 type Redis struct { - limiter *redis_rate.Limiter // limiter 限流实例 - redisClient *redisInstance.ClusterClient // redis 客户端实例 + limiter *redis_rate.Limiter // limiter 限流实例 + redisClient *redisInstance.Client // redis 客户端实例 lock *sync.RWMutex } -func (r *Redis) SetRedisClient(client *redisInstance.ClusterClient) { +func (r *Redis) SetRedisClient(client *redisInstance.Client) { r.redisClient = client r.limiter = redis_rate.NewLimiter(r.redisClient) } diff --git a/redis_test.go b/redis_test.go index c0b1e9e..6b8c6a0 100644 --- a/redis_test.go +++ b/redis_test.go @@ -14,16 +14,14 @@ import ( wrapRedis "git.zhangdeman.cn/zhangdeman/redis" wrapRedisDefine "git.zhangdeman.cn/zhangdeman/redis/define" "testing" + "time" ) func TestRedis_AllowN(t *testing.T) { - _ = wrapRedis.Client.AddClient("test_rate_limit", &wrapRedisDefine.ClusterOptions{ - Addrs: []string{"127.0.0.1:6379"}, + _ = wrapRedis.Client.AddClient("test_rate_limit", &wrapRedisDefine.Options{ + Addr: "127.0.0.1:6379", + Network: "TCP", ClientName: "", - MaxRedirects: 0, - ReadOnly: false, - RouteByLatency: false, - RouteRandomly: false, Protocol: 0, Username: "", Password: "123456", @@ -46,11 +44,14 @@ func TestRedis_AllowN(t *testing.T) { IdentitySuffix: "", }) RedisClient.SetRedisClient(wrapRedis.Client.GetRealClient("test_rate_limit")) - allow, err := RedisClient.AllowN(context.Background(), &define.LimitConfig{ - Key: "deman", - Total: 600, - Rate: 10, - TimeInterval: 60, - }, 8) - fmt.Println(allow, err) + for i := 0; i < 100; i++ { + allow, err := RedisClient.AllowN(context.Background(), &define.LimitConfig{ + Key: "deman", + Total: 600, + Rate: 10, + TimeInterval: 60, + }, 8) + fmt.Println(allow, err) + time.Sleep(time.Second) + } }