aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/lua/history_redis.lua
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2017-03-14 14:34:04 +0000
committerVsevolod Stakhov <vsevolod@highsecure.ru>2017-03-14 14:39:50 +0000
commit6c7258e2a58b835351a00fd8b3407b9655342051 (patch)
tree0e3637baa5398979f6aa422c99d05bfee3689ae7 /src/plugins/lua/history_redis.lua
parent43763bea31d65241263b23ff6fda3fc47f51bd6e (diff)
downloadrspamd-6c7258e2a58b835351a00fd8b3407b9655342051.tar.gz
rspamd-6c7258e2a58b835351a00fd8b3407b9655342051.zip
[Feature] Implement redis history querying
Diffstat (limited to 'src/plugins/lua/history_redis.lua')
-rw-r--r--src/plugins/lua/history_redis.lua71
1 files changed, 68 insertions, 3 deletions
diff --git a/src/plugins/lua/history_redis.lua b/src/plugins/lua/history_redis.lua
index a8184fca9..18cc9b5d6 100644
--- a/src/plugins/lua/history_redis.lua
+++ b/src/plugins/lua/history_redis.lua
@@ -69,7 +69,7 @@ end
local function history_save(task)
local function redis_llen_cb(err, _)
if err then
- rspamd_logger.errx(task, 'got error %s when writing history row',
+ rspamd_logger.errx(task, 'got error %s when writing history row: %s',
err)
end
end
@@ -100,14 +100,76 @@ local function history_save(task)
local ret, conn, _ = rspamd_redis_make_request(task,
redis_params, -- connect params
nil, -- hash key
- false, -- is write
+ true, -- is write
redis_llen_cb, --callback
'LPUSH', -- command
{prefix, json} -- arguments
)
if ret then
- conn:add_cmd('LTRIM', {settings.key_prefix, '0', tostring(settings.nrows)})
+ conn:add_cmd('LTRIM', {prefix, '0', tostring(settings.nrows)})
+ end
+end
+
+local function handle_history_request(task, conn, from, to, reset)
+ local prefix = settings.key_prefix
+ if settings.compress then
+ -- Distinguish between compressed and non-compressed options
+ prefix = prefix .. '_zst'
+ end
+
+ if reset then
+ local function redis_ltrim_cb(err, _)
+ if err then
+ rspamd_logger.errx(task, 'got error %s when resetting history: %s',
+ err)
+ conn:send_error(504, '{"error": "' .. err .. '"}')
+ else
+ conn:send_string('{"success":true}')
+ end
+ end
+ rspamd_redis_make_request(task,
+ redis_params, -- connect params
+ nil, -- hash key
+ true, -- is write
+ redis_ltrim_cb, --callback
+ 'LTRIM', -- command
+ {prefix, '0', '0'} -- arguments
+ )
+ else
+ local function redis_lrange_cb(err, data)
+ if data then
+ local reply = {
+ version = 2,
+ }
+
+ if settings.compress then
+ reply.rows = fun.totable(fun.filter(function(e) return e ~= nil end,
+ fun.map(function(e)
+ local _,dec = rspamd_util.zstd_decompress(e)
+ if dec then
+ return tostring(dec)
+ end
+ return nil
+ end, data)))
+ else
+ reply.rows = data
+ end
+ conn:send_ucl(reply)
+ else
+ rspamd_logger.errx(task, 'got error %s when getting history: %s',
+ err)
+ conn:send_error(504, '{"error": "' .. err .. '"}')
+ end
+ end
+ rspamd_redis_make_request(task,
+ redis_params, -- connect params
+ nil, -- hash key
+ false, -- is write
+ redis_lrange_cb, --callback
+ 'LRANGE', -- command
+ {prefix, tostring(from), tostring(to)} -- arguments
+ )
end
end
@@ -127,5 +189,8 @@ if opts then
callback = history_save,
priority = 150
})
+ rspamd_plugins['history'] = {
+ handler = handle_history_request
+ }
end
end \ No newline at end of file