# 由后端传入 local now = tonumber(ARGV[1]) local grade = ARGV[2]
# 定义各个用户等级的限流规则 # 根据用户的等级有不同的规则 # NORMAL 正常 # VIP # RISKY 危险 # 结构:rules[grade][dimension] = { {windowMillis, maxCount}, ... } local rules = {}
rules["NORMAL"] = { phone = { {60000, 1}, # 60s 内最多 1 条 {600000, 3}, # 10min 内最多 3 条 {3600000, 6}, # 1h 内最多 6 条 {86400000, 20} # 24h 内最多 20 条 }, user = { {600000, 5}, # 10min 内最多 5 条 {3600000, 10}, # 1h 内最多 10 条 {86400000, 30} # 24h 内最多 30 条 } }
rules["VIP"] = { phone = { {60000, 2}, # 60s 内最多 2 条 {600000, 5}, # 10min 内最多 5 条 {3600000, 10}, # 1h 内最多 10 条 {86400000, 40} # 24h 内最多 40 条 }, user = { {600000, 10}, # 10min 内最多 10 条 {3600000, 20}, # 1h 内最多 20 条 {86400000, 60} # 24h 内最多 60 条 } }
rules["RISKY"] = { phone = { {60000, 1}, # 60s 内最多 1 条 {600000, 2}, # 10min 内最多 2 条 {3600000, 3}, # 1h 内最多 3 条 {86400000, 5} # 24h 内最多 5 条 }, user = { {600000, 3}, # 10min 内最多 3 条 {3600000, 5}, # 1h 内最多 5 条 {86400000, 10} # 24h 内最多 10 条 } }
# 默认使用 NORMAL 配置 local conf = rules[grade] if conf == nil then conf = rules["NORMAL"] end
# 计算最长窗口,用于设置 key 的过期时间 local function maxWindowMillisForDimension(dimRules) local maxWindow = 0 for i, rule in ipairs(dimRules) do local window = rule[1] # 获取所有窗口的存在最长时间 if window > maxWindow then maxWindow = window end end return maxWindow end
local maxWindowMillis = 0 for _, dimRules in pairs(conf) do local dMax = maxWindowMillisForDimension(dimRules) if dMax > maxWindowMillis then maxWindowMillis = dMax end end
# 针对单个维度执行滑动窗口检查 # 返回:allowedFlag(0/1), waitSeconds local function checkDimension(key, dimRules) if key == nil or key == '' or dimRules == nil then return 1, 0 end
local dimMaxWait = 0
for i, rule in ipairs(dimRules) do # 获取窗口时间范围 local windowMillis = rule[1] # 窗口允许的最大请求量 local maxCount = rule[2] # 计算当前窗口的时间 local start = now - windowMillis
# 清理窗口外的数据 # ZREMRANGEBYSCORE key min max # 删除 ZSET 中 score 在 [min, max] 范围内的所有成员 # 返回值:删除的成员数量 redis.call("ZREMRANGEBYSCORE", key, 0, start) # 获取当前用户在窗口期内的请求数量 local count = redis.call("ZCARD", key) # 假如超过了允许请求量 if count >= maxCount then # 获取窗口内最早的一条记录用于计算等待时间 local oldest = redis.call("ZRANGE", key, 0, 0, "WITHSCORES") if oldest ~= nil and #oldest > 0 then local oldestScore = tonumber(oldest[2]) # 计算需要返回的等待时间 local waitMillis = oldestScore + windowMillis - now if waitMillis > 0 then local waitSeconds = math.floor((waitMillis + 999) / 1000) if waitSeconds > dimMaxWait then dimMaxWait = waitSeconds end end end end end
if dimMaxWait > 0 then return 0, dimMaxWait else return 1, 0 end end
# 从后端获取数据 local phoneKey = KEYS[1] local userKey = KEYS[2]
# 调用滑动窗口限流函数 local phoneAllowed, phoneWait = checkDimension(phoneKey, conf["phone"]) local userAllowed, userWait = checkDimension(userKey, conf["user"])
# 如有任一维度不允许,则整体不允许,返回最大等待时间 if phoneAllowed == 0 or userAllowed == 0 then local maxWait = phoneWait if userWait > maxWait then maxWait = userWait end return {0, maxWait} end
# 所有维度均允许时,写入当前记录,并设置合理的过期时间 if phoneKey ~= nil and phoneKey ~= '' then redis.call("ZADD", phoneKey, now, now) if maxWindowMillis > 0 then # 用于设置过期时间 local ttlSeconds = math.floor((maxWindowMillis * 2) / 1000) redis.call("EXPIRE", phoneKey, ttlSeconds) end end
if userKey ~= nil and userKey ~= '' then redis.call("ZADD", userKey, now, now) if maxWindowMillis > 0 then local ttlSeconds = math.floor((maxWindowMillis * 2) / 1000) redis.call("EXPIRE", userKey, ttlSeconds) end end
return {1, 0}
|