diff options
author | Vsevolod Stakhov <vsevolod@rspamd.com> | 2024-11-11 18:54:51 +0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-11-11 18:54:51 +0600 |
commit | afb9ccc0d5e59a293831ee70bc3f10f7a2ffbafb (patch) | |
tree | 5628dc17b0db75b319d7a49d85bf9a66354e7860 /lualib | |
parent | 95151295e8358136add594e2c5b048e79b19a130 (diff) | |
parent | 6140c9eabf1d5cb58da7f925f35bdd89749dca8a (diff) | |
download | rspamd-afb9ccc0d5e59a293831ee70bc3f10f7a2ffbafb.tar.gz rspamd-afb9ccc0d5e59a293831ee70bc3f10f7a2ffbafb.zip |
Merge pull request #5162 from left-try/master
Store LRU cache of last filled ratelimit buckets
Diffstat (limited to 'lualib')
-rw-r--r-- | lualib/redis_scripts/ratelimit_check.lua | 5 |
1 files changed, 5 insertions, 0 deletions
diff --git a/lualib/redis_scripts/ratelimit_check.lua b/lualib/redis_scripts/ratelimit_check.lua index f24e0daf0..019996c11 100644 --- a/lualib/redis_scripts/ratelimit_check.lua +++ b/lualib/redis_scripts/ratelimit_check.lua @@ -31,6 +31,8 @@ local leak_rate = tonumber(KEYS[3]) local max_burst = tonumber(KEYS[4]) local prefix = KEYS[1] local enable_dynamic = KEYS[7] == 'true' +local lfb_cache_prefix = KEYS[8] +local lfb_max_cache_size = tonumber(KEYS[9]) local dynr, dynb, leaked = 0, 0, 0 if not last then -- New bucket @@ -83,6 +85,9 @@ if burst + pending > 0 then burst = burst + pending if burst > 0 and burst > max_burst * dynb then + redis.call('ZREMRANGEBYRANK', lfb_cache_prefix, 0, -(lfb_max_cache_size + 1)) -- Keeping size of lfb cache + redis.call('ZADD', lfb_cache_prefix, now, prefix) -- LRU cache is based on timestamps of buckets + return { 1, tostring(burst - pending), tostring(dynr), tostring(dynb), tostring(leaked) } end -- Increase pending if we allow ratelimit |