From: Vsevolod Stakhov Date: Wed, 17 Jan 2024 15:08:19 +0000 (+0000) Subject: [Project] Some more work with regard to configuration X-Git-Tag: 3.8.0~8^2~1 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=da42b91ec31707fac4f9573867401002abbd35c5;p=rspamd.git [Project] Some more work with regard to configuration --- diff --git a/lualib/lua_bayes_redis.lua b/lualib/lua_bayes_redis.lua index 576f88b8a..daa4e6015 100644 --- a/lualib/lua_bayes_redis.lua +++ b/lualib/lua_bayes_redis.lua @@ -199,6 +199,7 @@ end local function gen_cache_learn_functor(redis_params, learn_script_id, conf) local packed_conf = ucl.to_format(conf, 'msgpack') return function(task, cache_id, is_spam) + local is_per_user = task:get_mempool():has_variable('stat_user') local function learn_redis_cb(err, data) lua_util.debugm(N, task, 'learn_cache redis cb: %s, %s', err, data) end @@ -207,7 +208,7 @@ local function gen_cache_learn_functor(redis_params, learn_script_id, conf) lua_redis.exec_redis_script(learn_script_id, { task = task, is_write = true, key = cache_id }, learn_redis_cb, - { cache_id, is_spam and "1" or "0", packed_conf }) + { cache_id, is_spam and "1" or "0", packed_conf, is_per_user and "1" or "0" }) end end diff --git a/lualib/redis_scripts/bayes_cache_check.lua b/lualib/redis_scripts/bayes_cache_check.lua index 3bea57d5f..f1ffc2b84 100644 --- a/lualib/redis_scripts/bayes_cache_check.lua +++ b/lualib/redis_scripts/bayes_cache_check.lua @@ -5,7 +5,7 @@ local cache_id = KEYS[1] local conf = cmsgpack.unpack(KEYS[2]) -cache_id = string.sub(cache_id, 1, conf.cache_prefix_len) +cache_id = string.sub(cache_id, 1, conf.cache_elt_len) -- Try each prefix that is in Redis for i = 0, conf.cache_max_keys do diff --git a/lualib/redis_scripts/bayes_cache_learn.lua b/lualib/redis_scripts/bayes_cache_learn.lua index 5335ce832..50cfae893 100644 --- a/lualib/redis_scripts/bayes_cache_learn.lua +++ b/lualib/redis_scripts/bayes_cache_learn.lua @@ -1,13 +1,15 @@ -- Lua script to perform cache checking for bayes classification -- This script accepts the following parameters: -- key1 - cache id --- key3 - is spam +-- key3 - is spam (1 or 0) -- key3 - configuration table in message pack +-- key4 - is per user (1 or 0) local cache_id = KEYS[1] local is_spam = KEYS[2] local conf = cmsgpack.unpack(KEYS[3]) -cache_id = string.sub(cache_id, 1, conf.cache_prefix_len) +local is_per_user = KEYS[4] == "1" +cache_id = string.sub(cache_id, 1, conf.cache_elt_len) -- Try each prefix that is in Redis (as some other instance might have set it) for i = 0, conf.cache_max_keys do @@ -21,12 +23,13 @@ for i = 0, conf.cache_max_keys do end local added = false +local lim = is_per_user and conf.cache_max_elt * conf.cache_per_user_mult or conf.cache_max_elt for i = 0, conf.cache_max_keys do if not added then local prefix = conf.cache_prefix .. string.rep("X", i) local count = redis.call('HLEN', prefix) - if count < conf.cache_max_elt then + if count < lim then -- We can add it to this prefix redis.call('HSET', prefix, cache_id, is_spam) added = true @@ -36,16 +39,23 @@ end if not added then -- Need to expire some keys + local expired = false for i = 0, conf.cache_max_keys do local prefix = conf.cache_prefix .. string.rep("X", i) local exists = redis.call('EXISTS', prefix) if exists then - redis.call('DEL', prefix) - redis.call('HSET', prefix, cache_id, is_spam) - - -- Do not expire anything else - return true + if expired then + redis.call('DEL', prefix) + redis.call('HSET', prefix, cache_id, is_spam) + + -- Do not expire anything else + expired = true + elseif i > 0 then + -- Move key to a shorter prefix, so we will rotate them eventually from lower to upper + local new_prefix = conf.cache_prefix .. string.rep("X", i - 1) + redis.call('RENAME', prefix, new_prefix) + end end end end