end
exports.rspamd_parse_redis_server = rspamd_parse_redis_server
+exports.parse_redis_server = rspamd_parse_redis_server
-- Performs async call to redis hiding all complexity inside function
-- task - rspamd_task
end
exports.rspamd_redis_make_request = rspamd_redis_make_request
+exports.redis_make_request = rspamd_redis_make_request
+
+local function redis_make_request_taskless(ev_base, cfg, key, is_write, callback, command, args)
+ if not ev_base or not redis_params or not callback or not command then
+ return false,nil,nil
+ end
+
+ local addr
+ local rspamd_redis = require "rspamd_redis"
+
+ if key then
+ if is_write then
+ addr = redis_params['write_servers']:get_upstream_by_hash(key)
+ else
+ addr = redis_params['read_servers']:get_upstream_by_hash(key)
+ end
+ else
+ if is_write then
+ addr = redis_params['write_servers']:get_upstream_master_slave(key)
+ else
+ addr = redis_params['read_servers']:get_upstream_round_robin(key)
+ end
+ end
+
+ if not addr then
+ rspamd_logger.errx(cfg, 'cannot select server to make redis request')
+ end
+
+ local options = {
+ ev_base = ev_base,
+ config = cfg,
+ callback = callback,
+ host = addr:get_addr(),
+ timeout = redis_params['timeout'],
+ cmd = command,
+ args = args
+ }
+
+ if redis_params['password'] then
+ options['password'] = redis_params['password']
+ end
+
+ if redis_params['db'] then
+ options['dbname'] = redis_params['db']
+ end
+
+ local ret,conn = rspamd_redis.make_request(options)
+ if not ret then
+ rspamd_logger.errx('cannot execute redis request')
+ end
+ return ret,conn,addr
+end
+
+exports.rspamd_redis_make_request_taskless = redis_make_request_taskless
+exports.redis_make_request_taskless = redis_make_request_taskless
local split_grammar = {}
function rspamd_str_split(s, sep)
local rspamd_tcp = require "rspamd_tcp"
local rspamd_url = require "rspamd_url"
local rspamd_util = require "rspamd_util"
+local globals = require "global_functions"
local check_local = false
local check_authed = false
local tz_offset = get_timezone_offset(os.time())
-local function redis_make_request(ev_base, cfg, key, is_write, callback, command, args)
- if not ev_base or not redis_params or not callback or not command then
- return false,nil,nil
- end
-
- local addr
- local rspamd_redis = require "rspamd_redis"
-
- if key then
- if is_write then
- addr = redis_params['write_servers']:get_upstream_by_hash(key)
- else
- addr = redis_params['read_servers']:get_upstream_by_hash(key)
- end
- else
- if is_write then
- addr = redis_params['write_servers']:get_upstream_master_slave(key)
- else
- addr = redis_params['read_servers']:get_upstream_round_robin(key)
- end
- end
-
- if not addr then
- rspamd_logger.errx(cfg, 'cannot select server to make redis request')
- end
-
- local options = {
- ev_base = ev_base,
- config = cfg,
- callback = callback,
- host = addr:get_addr(),
- timeout = redis_params['timeout'],
- cmd = command,
- args = args
- }
-
- if redis_params['password'] then
- options['password'] = redis_params['password']
- end
-
- if redis_params['db'] then
- options['dbname'] = redis_params['db']
- end
-
- local ret,conn = rspamd_redis.make_request(options)
- if not ret then
- rspamd_logger.errx(cfg, 'cannot execute redis request')
- end
- return ret,conn,addr
-end
-
local function load_scripts(cfg, ev_base)
local function redis_report_script_cb(err, data)
if err then
rspamd_logger.infox(cfg, 'Loaded DMARC report script with SHA %s', take_report_sha)
end
end
- local ret = redis_make_request(ev_base,
+ local ret = globals.redis_make_request_taskless(ev_base,
rspamd_config,
nil,
true, -- is write
local idx_key = table.concat({redis_keys.index_prefix, period}, redis_keys.join_char)
if report_data then
- local ret = rspamd_redis_make_request(task,
+ local ret = globals.redis_make_request(task,
redis_params, -- connect params
hfromdom, -- hash key
true, -- is write
dmarc_xml('push', {data[2][i], data[2][i+1]})
end
if cursor ~= 0 then
- local ret = redis_make_request(ev_base,
+ local ret = globals.redis_make_request_taskless(ev_base,
rspamd_config,
nil,
false, -- is write
end
end
end
- local ret = redis_make_request(ev_base,
+ local ret = globals.redis_make_request_taskless(ev_base,
rspamd_config,
nil,
false, -- is write
rspamd_logger.infox(rspamd_config, 'Deleted reports for %s', reporting_domain)
get_reporting_domain()
end
- local ret = redis_make_request(ev_base,
+ local ret = globals.redis_make_request_taskless(ev_base,
rspamd_config,
nil,
false, -- is write
end
end
local idx_key = table.concat({redis_keys.index_prefix, want_period}, redis_keys.join_char)
- local ret = redis_make_request(ev_base,
+ local ret = globals.redis_make_request_taskless(ev_base,
rspamd_config,
nil,
false, -- is write
local rspamd_util = require "rspamd_util"
local fann_symbol_spam = 'FANNR_SPAM'
local fann_symbol_ham = 'FANNR_HAM'
+local globals = require "global_functions"
local fun = require "fun"
local module_log_id = 0x200
local learning_spawned = false
local ann_expire = 60 * 60 * 24 * 2 -- 2 days
-local function redis_make_request(ev_base, cfg, key, is_write, callback, command, args)
- if not ev_base or not redis_params or not callback or not command then
- return false,nil,nil
- end
-
- local addr
- local rspamd_redis = require "rspamd_redis"
-
- if key then
- if is_write then
- addr = redis_params['write_servers']:get_upstream_by_hash(key)
- else
- addr = redis_params['read_servers']:get_upstream_by_hash(key)
- end
- else
- if is_write then
- addr = redis_params['write_servers']:get_upstream_master_slave(key)
- else
- addr = redis_params['read_servers']:get_upstream_round_robin(key)
- end
- end
-
- if not addr then
- rspamd_logger.errx(cfg, 'cannot select server to make redis request')
- end
-
- local options = {
- ev_base = ev_base,
- config = cfg,
- callback = callback,
- host = addr:get_addr(),
- timeout = redis_params['timeout'],
- cmd = command,
- args = args
- }
-
- if redis_params['password'] then
- options['password'] = redis_params['password']
- end
-
- if redis_params['db'] then
- options['dbname'] = redis_params['db']
- end
-
- local ret,conn = rspamd_redis.make_request(options)
- if not ret then
- rspamd_logger.errx('cannot execute redis request')
- end
- return ret,conn,addr
-end
-
local function load_scripts(cfg, ev_base, on_load_cb)
local function can_train_sha_cb(err, data)
if err or not data or type(data) ~= 'string' then
redis_can_train_sha = tostring(data)
end
end
- redis_make_request(ev_base,
+ globals.redis_make_request_taskless(ev_base,
rspamd_config,
nil,
true, -- is write
end
end
end
- redis_make_request(ev_base,
+ globals.redis_make_request_taskless(ev_base,
rspamd_config,
nil,
true, -- is write
redis_maybe_invalidate_sha = tostring(data)
end
end
- redis_make_request(ev_base,
+ globals.redis_make_request_taskless(ev_base,
rspamd_config,
nil,
true, -- is write
redis_locked_invalidate_sha = tostring(data)
end
end
- redis_make_request(ev_base,
+ globals.redis_make_request_taskless(ev_base,
rspamd_config,
nil,
true, -- is write
redis_maybe_lock_sha = tostring(data)
end
end
- redis_make_request(ev_base,
+ globals.redis_make_request_taskless(ev_base,
rspamd_config,
nil,
true, -- is write
redis_save_unlock_sha = tostring(data)
end
end
- redis_make_request(ev_base,
+ globals.redis_make_request_taskless(ev_base,
rspamd_config,
nil,
true, -- is write
end
-- Invalidate ANN
rspamd_logger.infox(rspamd_config, 'invalidate ANN %s', prefix)
- redis_make_request(ev_base,
+ globals.redis_make_request_taskless(ev_base,
rspamd_config,
nil,
true, -- is write
fun.each(function(e) table.insert(learn_data, e) end, extra)
local str = rspamd_util.zstd_compress(table.concat(learn_data, ';'))
- redis_make_request(ev_base,
+ globals.redis_make_request_taskless(ev_base,
rspamd_config,
nil,
true, -- is write
end
end
- redis_make_request(ev_base,
+ globals.redis_make_request_taskless(ev_base,
rspamd_config,
nil,
true, -- is write
if err then
rspamd_logger.errx(rspamd_config, 'cannot save ANN %s to redis: %s',
prefix, err)
- redis_make_request(ev_base,
+ globals.redis_make_request_taskless(ev_base,
rspamd_config,
nil,
false, -- is write
if errcode ~= 0 then
rspamd_logger.errx(rspamd_config, 'cannot train ANN %s: %s',
prefix, errmsg)
- redis_make_request(ev_base,
+ globals.redis_make_request_taskless(ev_base,
rspamd_config,
nil,
true, -- is write
fanns[elt].version = fanns[elt].version + 1
fanns[elt].fann = fanns[elt].fann_train
fanns[elt].fann_train = nil
- redis_make_request(ev_base,
+ globals.redis_make_request_taskless(ev_base,
rspamd_config,
nil,
true, -- is write
if err or type(data) ~= 'table' then
rspamd_logger.errx(rspamd_config, 'cannot get ham tokens for ANN %s from redis: %s',
prefix, err)
- redis_make_request(ev_base,
+ globals.redis_make_request_taskless(ev_base,
rspamd_config,
nil,
true, -- is write
end
-- Invalidate ANN
rspamd_logger.infox(rspamd_config, 'invalidate ANN %s: training data is invalid', prefix)
- redis_make_request(ev_base,
+ globals.redis_make_request_taskless(ev_base,
rspamd_config,
nil,
true, -- is write
if err or type(data) ~= 'table' then
rspamd_logger.errx(rspamd_config, 'cannot get spam tokens for ANN %s from redis: %s',
prefix, err)
- redis_make_request(ev_base,
+ globals.redis_make_request_taskless(ev_base,
rspamd_config,
nil,
true, -- is write
local _,str = rspamd_util.zstd_decompress(tok)
return fun.totable(fun.map(tonumber, rspamd_str_split(tostring(str), ';')))
end, data))
- redis_make_request(ev_base,
+ globals.redis_make_request_taskless(ev_base,
rspamd_config,
nil,
false, -- is write
end
elseif type(data) == 'number' then
-- Can train ANN
- redis_make_request(ev_base,
+ globals.redis_make_request_taskless(ev_base,
rspamd_config,
nil,
false, -- is write
end
end
if learning_spawned then
- redis_make_request(ev_base,
+ globals.redis_make_request_taskless(ev_base,
rspamd_config,
nil,
true, -- is write
rspamd_logger.infox(rspamd_config, 'do not learn ANN %s, already learning another ANN', prefix)
return
end
- redis_make_request(ev_base,
+ globals.redis_make_request_taskless(ev_base,
rspamd_config,
nil,
true, -- is write
end
end
- redis_make_request(ev_base,
+ globals.redis_make_request_taskless(ev_base,
rspamd_config,
nil,
false, -- is write
return 1.0
end
-- First we need to get all fanns stored in our Redis
- redis_make_request(ev_base,
+ globals.redis_make_request_taskless(ev_base,
rspamd_config,
nil,
false, -- is write
local_ver = fanns[elt].version
end
end
- redis_make_request(ev_base,
+ globals.redis_make_request_taskless(ev_base,
rspamd_config,
nil,
false, -- is write
return 1.0
end
-- First we need to get all fanns stored in our Redis
- redis_make_request(ev_base,
+ globals.redis_make_request_taskless(ev_base,
rspamd_config,
nil,
false, -- is write