rate_limit/redis_test.go

57 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"
)
func TestRedis_AllowN(t *testing.T) {
_ = wrapRedis.Client.AddClient("test_rate_limit", &wrapRedisDefine.ClusterOptions{
Addrs: []string{"127.0.0.1:6379"},
ClientName: "",
MaxRedirects: 0,
ReadOnly: false,
RouteByLatency: false,
RouteRandomly: false,
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"))
allow, err := RedisClient.AllowN(context.Background(), &define.LimitConfig{
Key: "deman",
Total: 600,
Rate: 10,
TimeInterval: 60,
}, 8)
fmt.Println(allow, err)
}