-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathredis_ratelimiter.go
66 lines (59 loc) · 1.81 KB
/
redis_ratelimiter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package ratelimiter
import (
"context"
"time"
"github.com/axiaoxin-com/logging"
"github.com/go-redis/redis/v8"
jsoniter "github.com/json-iterator/go"
"go.uber.org/zap"
)
var (
// RedisRatelimiterCacheExpiration redis ratelimiter 缓存过期时间
RedisRatelimiterCacheExpiration = time.Minute * 60
)
// RedisRatelimiter redis limiter
type RedisRatelimiter struct {
*redis.Client
script *redis.Script
}
// NewRedisRatelimiter 根据配置创建 redis limiter
func NewRedisRatelimiter(rdb *redis.Client) *RedisRatelimiter {
return &RedisRatelimiter{
Client: rdb,
script: tokenBucketRedisLuaIsLimitedScript,
}
}
// Allow 判断给定 key 是否被允许
func (r *RedisRatelimiter) Allow(ctx context.Context, key string, tokenFillInterval time.Duration, bucketSize int) bool {
// 参数小于等于 0 时直接限制
if tokenFillInterval.Seconds() <= 0 || bucketSize <= 0 {
return false
}
// 构造 lua 脚本参数
keys := []string{key}
args := []interface{}{
bucketSize,
1, // lua 脚本支持调整每次放入 token 的个数,这里全部统一使用每次放一个 token
tokenFillInterval.Microseconds(),
RedisRatelimiterCacheExpiration.Seconds(),
}
// 在 redis 中执行 lua 脚本计算当前 key 是否被限频
// Run 会自动使用 evalsha 优化带宽
v, err := r.script.Run(ctx, r.Client, keys, args...).Result()
if err != nil {
// 有 err 默认放行
logging.Error(ctx, "RedisRatelimiter run script error:"+err.Error())
return true
}
resultJSON, ok := v.(string)
if !ok {
logging.Error(ctx, "RedisRatelimiter assert script result error", zap.Any("result", v))
return true
}
isLimited := jsoniter.Get([]byte(resultJSON), "is_limited").ToBool()
// logging.Debug(ctx, "redis eval return json", zap.String("result", resultJSON))
if isLimited {
return false
}
return true
}