diff options
author | Vsevolod Stakhov <vsevolod@rspamd.com> | 2023-03-25 13:40:31 +0000 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@rspamd.com> | 2023-03-25 13:40:31 +0000 |
commit | dc9b70f7c3ce549c62288f025991765d0294facd (patch) | |
tree | 11f911958b9b51b475318c53d7ff0cb76fb45ea5 /lualib/redis_scripts/ratelimit_check.lua | |
parent | 5d8aa83eb61e59a2360e7d70e971217f8567d13a (diff) | |
download | rspamd-dc9b70f7c3ce549c62288f025991765d0294facd.tar.gz rspamd-dc9b70f7c3ce549c62288f025991765d0294facd.zip |
[Minor] Add some more comments as we now strip them
Diffstat (limited to 'lualib/redis_scripts/ratelimit_check.lua')
-rw-r--r-- | lualib/redis_scripts/ratelimit_check.lua | 36 |
1 files changed, 23 insertions, 13 deletions
diff --git a/lualib/redis_scripts/ratelimit_check.lua b/lualib/redis_scripts/ratelimit_check.lua index e43d34ce1..aa7f564ef 100644 --- a/lualib/redis_scripts/ratelimit_check.lua +++ b/lualib/redis_scripts/ratelimit_check.lua @@ -1,17 +1,27 @@ --- Checks bucket, updating it if needed --- KEYS[1] - prefix to update, e.g. RL_<triplet>_<seconds> --- KEYS[2] - current time in milliseconds --- KEYS[3] - bucket leak rate (messages per millisecond) --- KEYS[4] - bucket burst --- KEYS[5] - expire for a bucket --- KEYS[6] - number of recipients --- return 1 if message should be ratelimited and 0 if not +-- This Lua script is a rate limiter for Redis using the token bucket algorithm. +-- The script checks if a message should be rate-limited and updates the bucket status accordingly. +-- Input keys: +-- KEYS[1]: A prefix for the Redis keys, e.g., RL_<triplet>_<seconds> +-- KEYS[2]: The current time in milliseconds +-- KEYS[3]: The bucket leak rate (messages per millisecond) +-- KEYS[4]: The maximum allowed burst +-- KEYS[5]: The expiration time for a bucket +-- KEYS[6]: The number of recipients for the message + -- Redis keys used: --- l - last hit --- b - current burst --- p - pending messages (those that are currently processing) --- dr - current dynamic rate multiplier (*10000) --- db - current dynamic burst multiplier (*10000) +-- l: Last hit (time in milliseconds) +-- b: Current burst (number of tokens in the bucket) +-- p: Pending messages (number of messages in processing) +-- dr: Current dynamic rate multiplier (*10000) +-- db: Current dynamic burst multiplier (*10000) + +-- Returns: +-- An array containing: +-- 1. if the message should be rate-limited or 0 if not +-- 2. The current burst value after processing the message +-- 3. The dynamic rate multiplier +-- 4. The dynamic burst multiplier +-- 5. The number of tokens leaked during processing local last = redis.call('HGET', KEYS[1], 'l') local now = tonumber(KEYS[2]) |