Debian Codegraph @

lualib/redis_scripts/ratelimit_check.lua

raw
1 -- This Lua script is a rate limiter for Redis using the token bucket algorithm.
2 -- The script checks if a message should be rate-limited and updates the bucket status accordingly.
3 -- Input keys:
4 -- KEYS[1]: A prefix for the Redis keys, e.g., RL_<triplet>_<seconds>
5 -- KEYS[2]: The current time in milliseconds
6 -- KEYS[3]: The bucket leak rate (messages per millisecond)
7 -- KEYS[4]: The maximum allowed burst
8 -- KEYS[5]: The expiration time for a bucket
9 -- KEYS[6]: The number of recipients for the message
10 -- KEYS[7]: Enable dynamic ratelimits
11
12 -- Redis keys used:
13 -- l: Last hit (time in milliseconds)
14 -- b: Current burst (number of tokens in the bucket)
15 -- p: Pending messages (number of messages in processing)
16 -- dr: Current dynamic rate multiplier (*10000)
17 -- db: Current dynamic burst multiplier (*10000)
18
19 -- Returns:
20 -- An array containing:
21 -- 1. if the message should be rate-limited or 0 if not
22 -- 2. The current burst value after processing the message
23 -- 3. The dynamic rate multiplier
24 -- 4. The dynamic burst multiplier
25 -- 5. The number of tokens leaked during processing
26
27 local last = redis.call('HGET', KEYS[1], 'l')
28 local now = tonumber(KEYS[2])
29 local nrcpt = tonumber(KEYS[6])
30 local leak_rate = tonumber(KEYS[3])
31 local max_burst = tonumber(KEYS[4])
32 local prefix = KEYS[1]
33 local enable_dynamic = KEYS[7] == 'true'
34 local lfb_cache_prefix = KEYS[8]
35 local lfb_max_cache_size = tonumber(KEYS[9])
36 local dynr, dynb, leaked = 0, 0, 0
37 if not last then
38 -- New bucket
39 redis.call('HMSET', prefix, 'l', tostring(now), 'b', '0', 'dr', '10000', 'db', '10000', 'p', tostring(nrcpt))
40 redis.call('EXPIRE', prefix, KEYS[5])
41 return { 0, '0', '1', '1', '0' }
42 end
43 last = tonumber(last)
44
45 local burst, pending = unpack(redis.call('HMGET', prefix, 'b', 'p'))
46 burst, pending = tonumber(burst or '0'), tonumber(pending or '0')
47 -- Sanity to avoid races
48 if burst < 0 then
49 burst = 0
50 end
51 if pending < 0 then
52 pending = 0
53 end
54 pending = pending + nrcpt -- this message
55 -- Perform leak
56 if burst + pending > 0 then
57 -- If we have any time passed
58 if burst > 0 and last < now then
59 if enable_dynamic then
60 dynr = tonumber(redis.call('HGET', prefix, 'dr'))
61 if dynr then
62 dynr = dynr / 10000.0
63 if dynr == 0 then
64 dynr = 0.0001
65 end
66 else
67 dynr = 1.0
68 end
69 else
70 dynr = 1.0
71 end
72 leak_rate = leak_rate * dynr
73 leaked = ((now - last) * leak_rate)
74 if leaked > burst then
75 leaked = burst
76 end
77 burst = burst - leaked
78 redis.call('HINCRBYFLOAT', prefix, 'b', -(leaked))
79 redis.call('HSET', prefix, 'l', tostring(now))
80 end
81
82 if enable_dynamic then
83 dynb = tonumber(redis.call('HGET', prefix, 'db'))
84 if dynb then
85 dynb = dynb / 10000.0
86 if dynb == 0 then
87 dynb = 0.0001
88 end
89 else
90 dynb = 1.0
91 end
92 else
93 dynb = 1.0
94 end
95
96 burst = burst + pending
97 if burst > 0 and burst > max_burst * dynb then
98 redis.call('ZREMRANGEBYRANK', lfb_cache_prefix, 0, -(lfb_max_cache_size + 1)) -- Keeping size of lfb cache
99 redis.call('ZADD', lfb_cache_prefix, now, prefix) -- LRU cache is based on timestamps of buckets
100
101 return { 1, tostring(burst - pending), tostring(dynr), tostring(dynb), tostring(leaked) }
102 end
103 -- Increase pending if we allow ratelimit
104 redis.call('HINCRBY', prefix, 'p', nrcpt)
105 else
106 burst = 0
107 redis.call('HMSET', prefix, 'b', '0', 'p', tostring(nrcpt))
108 end
109
110 return { 0, tostring(burst), tostring(dynr), tostring(dynb), tostring(leaked) }