redis流控 + 单元测试

This commit is contained in:
2024-06-20 18:32:16 +08:00
parent e4bd913390
commit 54a77d170a
6 changed files with 217 additions and 55 deletions

View File

@ -7,7 +7,11 @@
// Date : 2024-06-20 11:33
package abstract
import "context"
import (
"context"
"git.zhangdeman.cn/zhangdeman/rate_limit/define"
"github.com/redis/go-redis/v9"
)
// IRateLimit 流控实现接口约束
//
@ -15,11 +19,14 @@ import "context"
//
// Date : 11:33 2024/6/20
type IRateLimit interface {
Second(ctx context.Context, key string, total, rate int) (bool, error) // 每秒访问次数限制
Minute(ctx context.Context, key string, total, rate int) (bool, error) // 每分钟访问次数限制
Hour(ctx context.Context, key string, total, rate int) (bool, error) // 每小时访问次数限制
Day(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) // 每年访问次数限制
Custom(ctx context.Context, timeInfo int64, key string, total, rate int) (bool, error) // 自定义指定时间内的访问次数, timeInfo 单位 : s
// Second(ctx context.Context, key string, total, rate int) (bool, error) // 每秒访问次数限制
// Minute(ctx context.Context, key string, total, rate int) (bool, error) // 每分钟访问次数限制
// Hour(ctx context.Context, key string, total, rate int) (bool, error) // 每小时访问次数限制
// Day(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) // 每年访问次数限制
// Custom(ctx context.Context, timeInfo int64, key string, total, rate int) (bool, error) // 自定义指定时间内的访问次数, timeInfo 单位 : s
SetRedisClient(client *redis.ClusterClient) // 设置redis客户端
AllowN(ctx context.Context, limitCfg *define.LimitConfig, tokenCnt int) (bool, error) // 申请N个令牌, N >= 0
Reset(ctx context.Context, limitCfg *define.LimitConfig) error // 重置
}