| 1 |
-- This script updates a token bucket rate limiter with dynamic rate and burst multipliers 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] - dynamic_rate_multiplier: A multiplier to adjust the rate limit dynamically. |
| 7 |
-- KEYS[4] - dynamic_burst_multiplier: A multiplier to adjust the burst limit dynamically. |
| 8 |
-- KEYS[5] - max_dyn_rate: The maximum allowed value for the dynamic rate multiplier. |
| 9 |
-- KEYS[6] - max_burst_rate: The maximum allowed value for the dynamic burst multiplier. |
| 10 |
-- KEYS[7] - expire: The expiration time for the Redis key storing the bucket information, in seconds. |
| 11 |
-- KEYS[8] - number_of_recipients: The number of requests to be allowed (or the increase rate). |
| 12 |
-- KEYS[9] - Enable dynamic ratelimits |
| 13 |
|
| 14 |
-- 1. Retrieve the last hit time and initialize variables |
| 15 |
local prefix = KEYS[1] |
| 16 |
local last = redis.call('HGET', prefix, 'l') |
| 17 |
local now = tonumber(KEYS[2]) |
| 18 |
local nrcpt = tonumber(KEYS[8]) |
| 19 |
local enable_dynamic = KEYS[9] == 'true' |
| 20 |
if not last then |
| 21 |
-- 2. Initialize a new bucket if the last hit time is not found (must not happen) |
| 22 |
redis.call('HMSET', prefix, 'l', tostring(now), 'b', tostring(nrcpt), 'dr', '10000', 'db', '10000', 'p', '0') |
| 23 |
redis.call('EXPIRE', prefix, KEYS[7]) |
| 24 |
return { 1, 1, 1 } |
| 25 |
end |
| 26 |
|
| 27 |
-- 3. Update the dynamic rate multiplier based on input parameters |
| 28 |
local dr, db = 1.0, 1.0 |
| 29 |
|
| 30 |
if enable_dynamic then |
| 31 |
local max_dr = tonumber(KEYS[5]) |
| 32 |
|
| 33 |
if max_dr > 1 then |
| 34 |
local rate_mult = tonumber(KEYS[3]) |
| 35 |
dr = tonumber(redis.call('HGET', prefix, 'dr')) |
| 36 |
if dr then |
| 37 |
dr = dr / 10000 |
| 38 |
else |
| 39 |
dr = 1.0 |
| 40 |
end |
| 41 |
|
| 42 |
if rate_mult > 1.0 and dr < max_dr then |
| 43 |
dr = dr * rate_mult |
| 44 |
if dr > 0.0001 then |
| 45 |
redis.call('HSET', prefix, 'dr', tostring(math.floor(dr * 10000))) |
| 46 |
else |
| 47 |
redis.call('HSET', prefix, 'dr', '1') |
| 48 |
end |
| 49 |
elseif rate_mult < 1.0 and dr > (1.0 / max_dr) then |
| 50 |
dr = dr * rate_mult |
| 51 |
if dr > 0.0001 then |
| 52 |
redis.call('HSET', prefix, 'dr', tostring(math.floor(dr * 10000))) |
| 53 |
else |
| 54 |
redis.call('HSET', prefix, 'dr', '1') |
| 55 |
end |
| 56 |
end |
| 57 |
end |
| 58 |
end |
| 59 |
|
| 60 |
-- 4. Update the dynamic burst multiplier based on input parameters |
| 61 |
if enable_dynamic then |
| 62 |
local max_db = tonumber(KEYS[6]) |
| 63 |
if max_db > 1 then |
| 64 |
local rate_mult = tonumber(KEYS[4]) |
| 65 |
db = tonumber(redis.call('HGET', prefix, 'db')) |
| 66 |
if db then |
| 67 |
db = db / 10000 |
| 68 |
else |
| 69 |
db = 1.0 |
| 70 |
end |
| 71 |
|
| 72 |
if rate_mult > 1.0 and db < max_db then |
| 73 |
db = db * rate_mult |
| 74 |
if db > 0.0001 then |
| 75 |
redis.call('HSET', prefix, 'db', tostring(math.floor(db * 10000))) |
| 76 |
else |
| 77 |
redis.call('HSET', prefix, 'db', '1') |
| 78 |
end |
| 79 |
elseif rate_mult < 1.0 and db > (1.0 / max_db) then |
| 80 |
db = db * rate_mult |
| 81 |
if db > 0.0001 then |
| 82 |
redis.call('HSET', prefix, 'db', tostring(math.floor(db * 10000))) |
| 83 |
else |
| 84 |
redis.call('HSET', prefix, 'db', '1') |
| 85 |
end |
| 86 |
end |
| 87 |
end |
| 88 |
end |
| 89 |
|
| 90 |
-- 5. Update the burst and pending values based on the number of recipients (requests) |
| 91 |
local burst, pending = unpack(redis.call('HMGET', prefix, 'b', 'p')) |
| 92 |
burst, pending = tonumber(burst or '0'), tonumber(pending or '0') |
| 93 |
if burst < 0 then |
| 94 |
burst = nrcpt |
| 95 |
else |
| 96 |
burst = burst + nrcpt |
| 97 |
end |
| 98 |
if pending < nrcpt then |
| 99 |
pending = 0 |
| 100 |
else |
| 101 |
pending = pending - nrcpt |
| 102 |
end |
| 103 |
|
| 104 |
-- 6. Set the updated values back to Redis and update the expiration time for the bucket |
| 105 |
redis.call('HMSET', prefix, 'b', tostring(burst), 'p', tostring(pending), 'l', KEYS[2]) |
| 106 |
redis.call('EXPIRE', prefix, KEYS[7]) |
| 107 |
|
| 108 |
-- 7. Return the updated burst value, dynamic rate multiplier, and dynamic burst multiplier |
| 109 |
return { tostring(burst), tostring(dr), tostring(db) } |