redis流控测试
This commit is contained in:
parent
54a77d170a
commit
545523e324
@ -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 // 重置
|
||||
}
|
||||
|
2
go.mod
2
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
|
||||
)
|
||||
|
2
go.sum
2
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=
|
||||
|
4
redis.go
4
redis.go
@ -27,11 +27,11 @@ var (
|
||||
// Date : 11:44 2024/6/20
|
||||
type Redis struct {
|
||||
limiter *redis_rate.Limiter // limiter 限流实例
|
||||
redisClient *redisInstance.ClusterClient // redis 客户端实例
|
||||
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)
|
||||
}
|
||||
|
@ -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,6 +44,7 @@ func TestRedis_AllowN(t *testing.T) {
|
||||
IdentitySuffix: "",
|
||||
})
|
||||
RedisClient.SetRedisClient(wrapRedis.Client.GetRealClient("test_rate_limit"))
|
||||
for i := 0; i < 100; i++ {
|
||||
allow, err := RedisClient.AllowN(context.Background(), &define.LimitConfig{
|
||||
Key: "deman",
|
||||
Total: 600,
|
||||
@ -53,4 +52,6 @@ func TestRedis_AllowN(t *testing.T) {
|
||||
TimeInterval: 60,
|
||||
}, 8)
|
||||
fmt.Println(allow, err)
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user