scripts.sliding_window_rate_limiter.lua Maven / Gradle / Ivy
local key = KEYS[1]
local member = KEYS[2]
local window_size = tonumber(ARGV[1])
local limit = tonumber(ARGV[2])
local timestamp = tonumber(ARGV[3])
local accepted = 0
local exists_key = redis.call('exists', key)
if (exists_key == 1) then
accepted = redis.call('zcard', key)
end
if (accepted < limit) then
redis.call('zadd', key, timestamp, member)
end
redis.call('zremrangebyscore', key, 0, timestamp - window_size)
redis.call('expire', key, window_size)
local remain = limit - accepted
local ttl = redis.call('ttl', key)
return { key, ttl, remain }
© 2015 - 2024 Weber Informatics LLC | Privacy Policy