rate_limit/redis_test.go

58 lines
1.5 KiB
Go
Raw Normal View History

2024-06-20 18:32:16 +08:00
// Package rate_limit ...
//
// Description : rate_limit ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2024-06-20 18:01
package rate_limit
import (
"context"
"fmt"
"git.zhangdeman.cn/zhangdeman/rate_limit/define"
wrapRedis "git.zhangdeman.cn/zhangdeman/redis"
wrapRedisDefine "git.zhangdeman.cn/zhangdeman/redis/define"
"testing"
2024-06-20 18:43:27 +08:00
"time"
2024-06-20 18:32:16 +08:00
)
func TestRedis_AllowN(t *testing.T) {
2024-06-20 18:43:27 +08:00
_ = wrapRedis.Client.AddClient("test_rate_limit", &wrapRedisDefine.Options{
Addr: "127.0.0.1:6379",
Network: "TCP",
2024-06-20 18:32:16 +08:00
ClientName: "",
Protocol: 0,
Username: "",
Password: "123456",
MaxRetries: 0,
MinRetryBackoff: 0,
MaxRetryBackoff: 0,
DialTimeout: 0,
ReadTimeout: 0,
WriteTimeout: 0,
ContextTimeoutEnabled: false,
PoolFIFO: false,
PoolSize: 0,
PoolTimeout: 0,
MinIdleConn: 0,
MaxIdleConn: 0,
MaxActiveConn: 0,
ConnMaxIdleTime: 0,
ConnMaxLifetime: 0,
DisableIdentity: false,
IdentitySuffix: "",
})
RedisClient.SetRedisClient(wrapRedis.Client.GetRealClient("test_rate_limit"))
2024-06-20 18:43:27 +08:00
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)
}
2024-06-20 18:32:16 +08:00
}