我们已经为我们的 Web 服务实现了基于 redis 的速率限制,该限制取自 here .我在这里复制相关代码。
local limits = cjson.decode(ARGV[1])
local now = tonumber(ARGV[2])
local weight = tonumber(ARGV[3] or '1')
local longest_duration = limits[1][1] or 0
local saved_keys = {}
-- handle cleanup and limit checks
for i, limit in ipairs(limits) do
local duration = limit[1]
longest_duration = math.max(longest_duration, duration)
local precision = limit[3] or duration
precision = math.min(precision, duration)
local blocks = math.ceil(duration / precision)
local saved = {}
table.insert(saved_keys, saved)
saved.block_id = math.floor(now / precision)
saved.trim_before = saved.block_id - blocks + 1
saved.count_key = duration .. ':' .. precision .. ':'
saved.ts_key = saved.count_key .. 'o'
for j, key in ipairs(KEYS) do
local old_ts = redis.call('HGET', key, saved.ts_key)
old_ts = old_ts and tonumber(old_ts) or saved.trim_before
if old_ts > now then
-- don't write in the past
return 1
end
-- discover what needs to be cleaned up
local decr = 0
local dele = {}
local trim = math.min(saved.trim_before, old_ts + blocks)
for old_block = old_ts, trim - 1 do
local bkey = saved.count_key .. old_block
local bcount = redis.call('HGET', key, bkey)
if bcount then
decr = decr + tonumber(bcount)
table.insert(dele, bkey)
end
end
-- handle cleanup
local cur
if #dele > 0 then
redis.call('HDEL', key, unpack(dele))
cur = redis.call('HINCRBY', key, saved.count_key, -decr)
else
cur = redis.call('HGET', key, saved.count_key)
end
-- check our limits
if tonumber(cur or '0') + weight > limit[2] then
return 1
end
end
end
我想弄清楚评论的含义 -- 不要写过去
我不知道在
old_ts
的情况下怎么可能大于 now
我已经把日志放在了 lua 代码中,但没有任何成功。最多
old_ts
可以等于 saved.trim_before
这又可以等于 now
如果 precision
是 1 和 blocks
是 1. 但不是更大。如果有人对此有所了解,这将很有帮助。
最佳答案
如果您查看文章中提供的要点
https://gist.github.com/josiahcarlson/80584b49da41549a7d5c
有评论问
In over_limit_sliding_window_lua_, should
if old_ts > now then
at here be
if old_ts > saved.block_id then
我同意这一点,
old_ts
应该有 bucket
而当bucket跳转到下一个slot时,也就是old_ts
的时候将大于 block_id
关于lua - 使用 lua 进行速率限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56885604/