forked from avinassh/ratelimit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsliding_window.lua
More file actions
27 lines (24 loc) · 853 Bytes
/
sliding_window.lua
File metadata and controls
27 lines (24 loc) · 853 Bytes
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
local key = KEYS[1]
local rate = tonumber(ARGV[1])
local window = tonumber(ARGV[2])
local now = tonumber(ARGV[3])
local old_window = now - window
local default_expiry = window * 5
local function set(ts, counter)
redis.call("ZADD", key, ts, ts)
redis.call("EXPIRE", key, default_expiry)
return {"ts", ts, "c", counter, "s", 1}
end
local function run()
-- remove all the old window scores
redis.call("ZREMRANGEBYSCORE", key, "-inf", old_window)
local counter = redis.call("ZCARD", key)
if counter < rate then
return set(now, counter+1)
end
-- the limit has reached, so we just return the counter values
-- the oldest record in the set gives us the last refill time
local last_refill = tonumber(redis.call("ZRANGE", key, 0, 0)[1])
return {"ts", last_refill, "c", counter, "s", 0}
end
return run()