调整流控策略redis key过期时间

This commit is contained in:
Liujian
2026-01-30 11:02:16 +08:00
parent 740fd1d107
commit c924f2b95c
5 changed files with 44 additions and 10 deletions
+2 -2
View File
@@ -28,7 +28,7 @@ if #to_del > 0 then
redis.call('HDEL', key, unpack(to_del))
end
-- 设置过期(粗略,2倍窗口 + 裕度
redis.call('EXPIRE', key, 2 * (curr_idx - win_start + 100))
-- ARGV[#ARGV] 是 Go 传进来的 ttl 秒数(最后一个参数
redis.call('EXPIRE', KEYS[1], ARGV[#ARGV])
return new_val
@@ -44,7 +44,7 @@ if #to_del > 0 then
redis.call('HDEL', key, unpack(to_del))
end
-- 可选:设置过期时间(窗口长度 + 一点裕度
redis.call('EXPIRE', key, 2 * (curr_idx - win_start + 100) ) -- 粗略,实际可更精确
-- ARGV[#ARGV] 是 Go 传进来的 ttl 秒数(最后一个参数
redis.call('EXPIRE', KEYS[1], ARGV[#ARGV])
return {new_sum, 1}
-3
View File
@@ -23,7 +23,4 @@ if #to_del > 0 then
redis.call('HDEL', key, unpack(to_del))
end
-- 可选:刷新过期时间
redis.call('EXPIRE', key, 2 * (win_start + 1000 - win_start)) -- 简单示例
return sum
+38
View File
@@ -51,6 +51,42 @@ func init() {
addLua = string(script)
}
// 根据 step 判断粒度并返回保守的 multiplier(用于 ttl = window_sec * multiplier
func (v *Vector) getExpireMultiplier() int {
stepSec := v.step / int64(time.Second)
switch {
case stepSec == 1: // 秒级
return 8 // 8倍 + 裕度,够覆盖抖动
case stepSec >= 60 && stepSec < 3600: // 分钟级(60s ~ 59min
return 6
case stepSec >= 3600: // 小时级及以上
return 4
default:
return 6 // 默认偏保守
}
}
// 计算窗口总秒数
func (v *Vector) windowSeconds() int64 {
return v.size * v.step / int64(time.Second)
}
// 获取最终的 TTL 秒数(可直接传给 Lua 的最后一个 ARGV)
func (v *Vector) getTTLSeconds() int {
winSec := v.windowSeconds()
if winSec <= 0 {
return 3600 // 防止异常
}
mult := v.getExpireMultiplier()
// 额外加一点固定裕度(防止边界情况)
extra := int64(600) // 10分钟
if winSec > 3600*24 {
extra = 86400 // 大窗口再加1天
}
return int(winSec*int64(mult) + extra)
}
// CompareAndAdd 去锁版本(推荐)
func (v *Vector) CompareAndAdd(ctx context.Context, key string, threshold, delta int64) (int64, bool) {
token := fmt.Sprintf("strategy-limiting:%s:%s", v.name, key)
@@ -63,6 +99,7 @@ func (v *Vector) CompareAndAdd(ctx context.Context, key string, threshold, delta
strconv.FormatInt(bucketStart, 10),
strconv.FormatInt(threshold, 10),
strconv.FormatInt(delta, 10),
strconv.Itoa(v.getTTLSeconds()),
}
// 使用 EVAL 执行
@@ -92,6 +129,7 @@ func (v *Vector) Add(ctx context.Context, key string, delta int64) int64 {
strconv.FormatInt(index, 10),
strconv.FormatInt(bucketStart, 10),
strconv.FormatInt(delta, 10),
strconv.Itoa(v.getTTLSeconds()),
}
result, err := v.cmd.Eval(ctx, addLua, []string{token}, args...).Result()
+2 -3
View File
@@ -9,7 +9,7 @@ require (
github.com/clbanning/mxj v1.8.4
github.com/coocood/freecache v1.2.2
github.com/dubbogo/gost v1.13.1
github.com/eolinker/eosc v0.21.4
github.com/eolinker/eosc v0.21.5
github.com/fasthttp/websocket v1.5.0
github.com/fullstorydev/grpcurl v1.8.7
github.com/google/uuid v1.6.0
@@ -43,7 +43,6 @@ require (
require (
github.com/cenkalti/backoff/v4 v4.2.1
github.com/go-redsync/redsync/v4 v4.15.0
github.com/tidwall/gjson v1.18.0
github.com/tidwall/sjson v1.2.5
github.com/tjfoc/gmsm v1.4.1
@@ -232,4 +231,4 @@ require (
replace github.com/soheilhy/cmux v0.1.5 => github.com/hmzzrcs/cmux v0.1.6
replace github.com/eolinker/eosc => ../eosc
//replace github.com/eolinker/eosc => ../eosc