Debian Codegraph @

lualib/redis_scripts/ratelimit_cleanup_pending.lua

raw
1 -- This script cleans up the pending requests in Redis.
2
3 -- KEYS: Input parameters
4 -- KEYS[1] - prefix: The Redis key prefix used to store the bucket information.
5 -- KEYS[2] - now: The current time in milliseconds.
6 -- KEYS[3] - expire: The expiration time for the Redis key storing the bucket information, in seconds.
7 -- KEYS[4] - number_of_recipients: The number of requests to be allowed (or the increase rate).
8
9 -- 1. Retrieve the last hit time and initialize variables
10 local prefix = KEYS[1]
11 local last = redis.call('HGET', prefix, 'l')
12 local nrcpt = tonumber(KEYS[4])
13 if not last then
14 -- No bucket, no cleanup
15 return 0
16 end
17
18
19 -- 2. Update the pending values based on the number of recipients (requests)
20 local pending = redis.call('HGET', prefix, 'p')
21 pending = tonumber(pending or '0')
22 if pending < nrcpt then
23 pending = 0
24 else
25 pending = pending - nrcpt
26 end
27
28 -- 3. Set the updated values back to Redis and update the expiration time for the bucket
29 redis.call('HMSET', prefix, 'p', tostring(pending), 'l', KEYS[2])
30 redis.call('EXPIRE', prefix, KEYS[3])
31
32 -- 4. Return the updated pending value
33 return pending