添加redis/内存流控实现 #1

Merged
zhangdeman merged 6 commits from feature/init_version into master 2024-06-20 20:59:14 +08:00
5 changed files with 21 additions and 18 deletions
Showing only changes of commit 545523e324 - Show all commits

View File

@ -26,7 +26,7 @@ type IRateLimit interface {
// Month(ctx context.Context, key string, total, rate int) (bool, error) // 每月访问次数限制 // Month(ctx context.Context, key string, total, rate int) (bool, error) // 每月访问次数限制
// Year(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 // 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 AllowN(ctx context.Context, limitCfg *define.LimitConfig, tokenCnt int) (bool, error) // 申请N个令牌, N >= 0
Reset(ctx context.Context, limitCfg *define.LimitConfig) error // 重置 Reset(ctx context.Context, limitCfg *define.LimitConfig) error // 重置
} }

2
go.mod
View File

@ -3,7 +3,7 @@ module git.zhangdeman.cn/zhangdeman/rate_limit
go 1.22.4 go 1.22.4
require ( 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/go-redis/redis_rate/v10 v10.0.1
github.com/redis/go-redis/v9 v9.5.3 github.com/redis/go-redis/v9 v9.5.3
) )

2
go.sum
View File

@ -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-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 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-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 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-20240325080031-1f58204e8687/go.mod h1:gf7SW2TXATgux8pfdFedMkXWv2515OtIIM/5c4atkFw=
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240618035451-8d48a6bd39dd h1:2Y37waOVCmVvx0Rp8VGEptE2/2JVMImtxB4dKKDk/3w= git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240618035451-8d48a6bd39dd h1:2Y37waOVCmVvx0Rp8VGEptE2/2JVMImtxB4dKKDk/3w=

View File

@ -27,11 +27,11 @@ var (
// Date : 11:44 2024/6/20 // Date : 11:44 2024/6/20
type Redis struct { type Redis struct {
limiter *redis_rate.Limiter // limiter 限流实例 limiter *redis_rate.Limiter // limiter 限流实例
redisClient *redisInstance.ClusterClient // redis 客户端实例 redisClient *redisInstance.Client // redis 客户端实例
lock *sync.RWMutex lock *sync.RWMutex
} }
func (r *Redis) SetRedisClient(client *redisInstance.ClusterClient) { func (r *Redis) SetRedisClient(client *redisInstance.Client) {
r.redisClient = client r.redisClient = client
r.limiter = redis_rate.NewLimiter(r.redisClient) r.limiter = redis_rate.NewLimiter(r.redisClient)
} }

View File

@ -14,16 +14,14 @@ import (
wrapRedis "git.zhangdeman.cn/zhangdeman/redis" wrapRedis "git.zhangdeman.cn/zhangdeman/redis"
wrapRedisDefine "git.zhangdeman.cn/zhangdeman/redis/define" wrapRedisDefine "git.zhangdeman.cn/zhangdeman/redis/define"
"testing" "testing"
"time"
) )
func TestRedis_AllowN(t *testing.T) { func TestRedis_AllowN(t *testing.T) {
_ = wrapRedis.Client.AddClient("test_rate_limit", &wrapRedisDefine.ClusterOptions{ _ = wrapRedis.Client.AddClient("test_rate_limit", &wrapRedisDefine.Options{
Addrs: []string{"127.0.0.1:6379"}, Addr: "127.0.0.1:6379",
Network: "TCP",
ClientName: "", ClientName: "",
MaxRedirects: 0,
ReadOnly: false,
RouteByLatency: false,
RouteRandomly: false,
Protocol: 0, Protocol: 0,
Username: "", Username: "",
Password: "123456", Password: "123456",
@ -46,6 +44,7 @@ func TestRedis_AllowN(t *testing.T) {
IdentitySuffix: "", IdentitySuffix: "",
}) })
RedisClient.SetRedisClient(wrapRedis.Client.GetRealClient("test_rate_limit")) RedisClient.SetRedisClient(wrapRedis.Client.GetRealClient("test_rate_limit"))
for i := 0; i < 100; i++ {
allow, err := RedisClient.AllowN(context.Background(), &define.LimitConfig{ allow, err := RedisClient.AllowN(context.Background(), &define.LimitConfig{
Key: "deman", Key: "deman",
Total: 600, Total: 600,
@ -53,4 +52,6 @@ func TestRedis_AllowN(t *testing.T) {
TimeInterval: 60, TimeInterval: 60,
}, 8) }, 8)
fmt.Println(allow, err) fmt.Println(allow, err)
time.Sleep(time.Second)
}
} }