File: sliding_window.lua

package info (click to toggle)
python-limits 4.4.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,064 kB
  • sloc: python: 7,833; makefile: 162; sh: 59
file content (17 lines) | stat: -rw-r--r-- 713 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
local expiry = tonumber(ARGV[1]) * 1000
local previous_count = redis.call('get', KEYS[1])
local previous_ttl = redis.call('pttl', KEYS[1])
local current_count = redis.call('get', KEYS[2])
local current_ttl = redis.call('pttl', KEYS[2])

if current_ttl > 0 and current_ttl < expiry then
    -- Current window expired, shift it to the previous window
    redis.call('rename', KEYS[2], KEYS[1])
    redis.call('set', KEYS[2], 0, 'PX', current_ttl + expiry)
    previous_count = redis.call('get', KEYS[1])
    previous_ttl = redis.call('pttl', KEYS[1])
    current_count = redis.call('get', KEYS[2])
    current_ttl = redis.call('pttl', KEYS[2])
end

return {previous_count, previous_ttl, current_count, current_ttl}