diff options
author | Vsevolod Stakhov <vsevolod@rspamd.com> | 2023-08-07 11:41:28 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@rspamd.com> | 2023-08-07 11:41:28 +0100 |
commit | 662145d0554de5e769b92dab2d41173a98adcee5 (patch) | |
tree | ec28311a0bce6181f248ba7b50304293ad764e44 /lualib/redis_scripts | |
parent | bbd88232db43d18f5e0de5a6502848d4074621c5 (diff) | |
download | rspamd-662145d0554de5e769b92dab2d41173a98adcee5.tar.gz rspamd-662145d0554de5e769b92dab2d41173a98adcee5.zip |
[Minor] Reformat all Lua code, no functional changes
Diffstat (limited to 'lualib/redis_scripts')
-rw-r--r-- | lualib/redis_scripts/neural_maybe_invalidate.lua | 2 | ||||
-rw-r--r-- | lualib/redis_scripts/neural_maybe_lock.lua | 2 | ||||
-rw-r--r-- | lualib/redis_scripts/neural_train_size.lua | 10 | ||||
-rw-r--r-- | lualib/redis_scripts/ratelimit_check.lua | 30 | ||||
-rw-r--r-- | lualib/redis_scripts/ratelimit_cleanup_pending.lua | 6 | ||||
-rw-r--r-- | lualib/redis_scripts/ratelimit_update.lua | 20 |
6 files changed, 48 insertions, 22 deletions
diff --git a/lualib/redis_scripts/neural_maybe_invalidate.lua b/lualib/redis_scripts/neural_maybe_invalidate.lua index c54871717..517fa019d 100644 --- a/lualib/redis_scripts/neural_maybe_invalidate.lua +++ b/lualib/redis_scripts/neural_maybe_invalidate.lua @@ -8,7 +8,7 @@ local lim = tonumber(KEYS[2]) if card > lim then local to_delete = redis.call('ZRANGE', KEYS[1], 0, card - lim - 1) if to_delete then - for _,k in ipairs(to_delete) do + for _, k in ipairs(to_delete) do local tb = cjson.decode(k) if type(tb) == 'table' and type(tb.redis_key) == 'string' then redis.call('DEL', tb.redis_key) diff --git a/lualib/redis_scripts/neural_maybe_lock.lua b/lualib/redis_scripts/neural_maybe_lock.lua index 7b5c6a60f..f705115b0 100644 --- a/lualib/redis_scripts/neural_maybe_lock.lua +++ b/lualib/redis_scripts/neural_maybe_lock.lua @@ -11,7 +11,7 @@ if locked then locked = tonumber(locked) local expire = tonumber(KEYS[3]) if now > locked and (now - locked) < expire then - return {tostring(locked), redis.call('HGET', KEYS[1], 'hostname') or 'unknown'} + return { tostring(locked), redis.call('HGET', KEYS[1], 'hostname') or 'unknown' } end end redis.call('HSET', KEYS[1], 'lock', tostring(now)) diff --git a/lualib/redis_scripts/neural_train_size.lua b/lualib/redis_scripts/neural_train_size.lua index 5a00ae3fc..45ad6a931 100644 --- a/lualib/redis_scripts/neural_train_size.lua +++ b/lualib/redis_scripts/neural_train_size.lua @@ -13,8 +13,12 @@ local nspam = 0 local nham = 0 local ret = redis.call('SCARD', prefix .. '_spam_set') -if ret then nspam = tonumber(ret) end +if ret then + nspam = tonumber(ret) +end ret = redis.call('SCARD', prefix .. '_ham_set') -if ret then nham = tonumber(ret) end +if ret then + nham = tonumber(ret) +end -return {nspam,nham}
\ No newline at end of file +return { nspam, nham }
\ No newline at end of file diff --git a/lualib/redis_scripts/ratelimit_check.lua b/lualib/redis_scripts/ratelimit_check.lua index 1c2b32a69..d39cdf148 100644 --- a/lualib/redis_scripts/ratelimit_check.lua +++ b/lualib/redis_scripts/ratelimit_check.lua @@ -34,36 +34,46 @@ if not last then -- New bucket redis.call('HMSET', prefix, 'l', tostring(now), 'b', '0', 'dr', '10000', 'db', '10000', 'p', tostring(nrcpt)) redis.call('EXPIRE', prefix, KEYS[5]) - return {0, '0', '1', '1', '0'} + return { 0, '0', '1', '1', '0' } end last = tonumber(last) -local burst,pending = unpack(redis.call('HMGET', prefix, 'b', 'p')) -burst,pending = tonumber(burst or '0'),tonumber(pending or '0') +local burst, pending = unpack(redis.call('HMGET', prefix, 'b', 'p')) +burst, pending = tonumber(burst or '0'), tonumber(pending or '0') -- Sanity to avoid races -if burst < 0 then burst = 0 end -if pending < 0 then pending = 0 end +if burst < 0 then + burst = 0 +end +if pending < 0 then + pending = 0 +end pending = pending + nrcpt -- this message -- Perform leak if burst + pending > 0 then -- If we have any time passed if burst > 0 and last < now then dynr = tonumber(redis.call('HGET', prefix, 'dr')) / 10000.0 - if dynr == 0 then dynr = 0.0001 end + if dynr == 0 then + dynr = 0.0001 + end leak_rate = leak_rate * dynr leaked = ((now - last) * leak_rate) - if leaked > burst then leaked = burst end + if leaked > burst then + leaked = burst + end burst = burst - leaked redis.call('HINCRBYFLOAT', prefix, 'b', -(leaked)) redis.call('HSET', prefix, 'l', tostring(now)) end dynb = tonumber(redis.call('HGET', prefix, 'db')) / 10000.0 - if dynb == 0 then dynb = 0.0001 end + if dynb == 0 then + dynb = 0.0001 + end burst = burst + pending if burst > 0 and burst > max_burst * dynb then - return {1, tostring(burst - pending), tostring(dynr), tostring(dynb), tostring(leaked)} + return { 1, tostring(burst - pending), tostring(dynr), tostring(dynb), tostring(leaked) } end -- Increase pending if we allow ratelimit redis.call('HINCRBY', prefix, 'p', nrcpt) @@ -72,4 +82,4 @@ else redis.call('HMSET', prefix, 'b', '0', 'p', tostring(nrcpt)) end -return {0, tostring(burst), tostring(dynr), tostring(dynb), tostring(leaked)}
\ No newline at end of file +return { 0, tostring(burst), tostring(dynr), tostring(dynb), tostring(leaked) }
\ No newline at end of file diff --git a/lualib/redis_scripts/ratelimit_cleanup_pending.lua b/lualib/redis_scripts/ratelimit_cleanup_pending.lua index 67ae634f1..698a3ec19 100644 --- a/lualib/redis_scripts/ratelimit_cleanup_pending.lua +++ b/lualib/redis_scripts/ratelimit_cleanup_pending.lua @@ -19,7 +19,11 @@ end -- 2. Update the pending values based on the number of recipients (requests) local pending = redis.call('HGET', prefix, 'p') pending = tonumber(pending or '0') -if pending < nrcpt then pending = 0 else pending = pending - nrcpt end +if pending < nrcpt then + pending = 0 +else + pending = pending - nrcpt +end -- 3. Set the updated values back to Redis and update the expiration time for the bucket redis.call('HMSET', prefix, 'p', tostring(pending), 'l', KEYS[2]) diff --git a/lualib/redis_scripts/ratelimit_update.lua b/lualib/redis_scripts/ratelimit_update.lua index f08a250ea..caee8fb31 100644 --- a/lualib/redis_scripts/ratelimit_update.lua +++ b/lualib/redis_scripts/ratelimit_update.lua @@ -19,7 +19,7 @@ if not last then -- 2. Initialize a new bucket if the last hit time is not found (must not happen) redis.call('HMSET', prefix, 'l', tostring(now), 'b', tostring(nrcpt), 'dr', '10000', 'db', '10000', 'p', '0') redis.call('EXPIRE', prefix, KEYS[7]) - return {1, 1, 1} + return { 1, 1, 1 } end -- 3. Update the dynamic rate multiplier based on input parameters @@ -72,14 +72,22 @@ if max_db > 1 then end -- 5. Update the burst and pending values based on the number of recipients (requests) -local burst,pending = unpack(redis.call('HMGET', prefix, 'b', 'p')) -burst,pending = tonumber(burst or '0'),tonumber(pending or '0') -if burst < 0 then burst = nrcpt else burst = burst + nrcpt end -if pending < nrcpt then pending = 0 else pending = pending - nrcpt end +local burst, pending = unpack(redis.call('HMGET', prefix, 'b', 'p')) +burst, pending = tonumber(burst or '0'), tonumber(pending or '0') +if burst < 0 then + burst = nrcpt +else + burst = burst + nrcpt +end +if pending < nrcpt then + pending = 0 +else + pending = pending - nrcpt +end -- 6. Set the updated values back to Redis and update the expiration time for the bucket redis.call('HMSET', prefix, 'b', tostring(burst), 'p', tostring(pending), 'l', KEYS[2]) redis.call('EXPIRE', prefix, KEYS[7]) -- 7. Return the updated burst value, dynamic rate multiplier, and dynamic burst multiplier -return {tostring(burst), tostring(dr), tostring(db)}
\ No newline at end of file +return { tostring(burst), tostring(dr), tostring(db) }
\ No newline at end of file |