summaryrefslogtreecommitdiffstats
path: root/lualib
diff options
context:
space:
mode:
authorMikhail Galanin <mgalanin@mimecast.com>2018-09-11 11:07:58 +0100
committerMikhail Galanin <mgalanin@mimecast.com>2018-09-11 11:07:58 +0100
commit09551bcf6ad0033e5e56e53b9d2e7b05d4d91996 (patch)
tree31284f84ec39c68c672ffb8ddf4f9a205573bd02 /lualib
parent2ee5f7e4cdadb324aa16af67491f56a1f6fdb412 (diff)
parent36aac959a763a1fdd82b9056b43962d4ea898e38 (diff)
downloadrspamd-09551bcf6ad0033e5e56e53b9d2e7b05d4d91996.tar.gz
rspamd-09551bcf6ad0033e5e56e53b9d2e7b05d4d91996.zip
Merge branch 'master' into redis-coroutines
Diffstat (limited to 'lualib')
-rw-r--r--lualib/lua_redis.lua90
-rw-r--r--lualib/lua_selectors.lua17
-rw-r--r--lualib/lua_util.lua19
3 files changed, 111 insertions, 15 deletions
diff --git a/lualib/lua_redis.lua b/lualib/lua_redis.lua
index 3c7bcd3b6..e9ef33a9b 100644
--- a/lualib/lua_redis.lua
+++ b/lualib/lua_redis.lua
@@ -1029,4 +1029,94 @@ end
exports.redis_connect_sync = redis_connect_sync
+--[[[
+-- @function lua_redis.request(redis_params, attrs, req)
+-- Sends a request to Redis (modern API)
+-- @param redis_params a table of redis server parameters
+-- @param attrs a table of redis request attributes (e.g. task, or ev_base + cfg + session)
+-- @param req a table of request: a command + command options
+--]]
+
+exports.request = function(redis_params, attrs, req)
+ local lua_util = require "lua_util"
+
+ if not attrs or not redis_params or not req then
+ logger.errx('invalid arguments for redis request')
+ return false,nil,nil
+ end
+
+ if not (attrs.task or (attrs.config and attrs.ev_base)) then
+ logger.errx('invalid attributes for redis request')
+ return false,nil,nil
+ end
+
+ local opts = lua_util.shallowcopy(attrs)
+
+ local log_obj = opts.task or opts.config
+
+ local addr
+
+ if opts.callback then
+ -- Wrap callback
+ local callback = opts.callback
+ local function rspamd_redis_make_request_cb(err, data)
+ if err then
+ addr:fail()
+ else
+ addr:ok()
+ end
+ callback(err, data, addr)
+ end
+ opts.callback = rspamd_redis_make_request_cb
+ end
+
+ local rspamd_redis = require "rspamd_redis"
+ local is_write = opts.is_write
+
+ if opts.key then
+ if is_write then
+ addr = redis_params['write_servers']:get_upstream_by_hash(attrs.key)
+ else
+ addr = redis_params['read_servers']:get_upstream_by_hash(attrs.key)
+ end
+ else
+ if is_write then
+ addr = redis_params['write_servers']:get_upstream_master_slave(attrs.key)
+ else
+ addr = redis_params['read_servers']:get_upstream_round_robin(attrs.key)
+ end
+ end
+
+ if not addr then
+ logger.errx(log_obj, 'cannot select server to make redis request')
+ end
+
+ opts.host = addr:get_addr()
+ opts.timeout = redis_params.timeout
+
+ if type(req) == 'string' then
+ opts.cmd = req
+ else
+ -- XXX: modifies the input table
+ opts.cmd = table.remove(req, 1);
+ opts.args = req
+ end
+
+ if redis_params.password then
+ opts.password = redis_params.password
+ end
+
+ if redis_params.db then
+ opts.dbname = redis_params.db
+ end
+
+ local ret,conn = rspamd_redis.make_request(opts)
+ if not ret then
+ logger.errx(log_obj, 'cannot execute redis request')
+ addr:fail()
+ end
+
+ return ret,conn,addr
+end
+
return exports
diff --git a/lualib/lua_selectors.lua b/lualib/lua_selectors.lua
index d1ef91230..112443fd3 100644
--- a/lualib/lua_selectors.lua
+++ b/lualib/lua_selectors.lua
@@ -536,19 +536,6 @@ exports.parse_selector = function(cfg, str)
local output = {}
if not parsed then return nil end
- local function shallowcopy(orig)
- local orig_type = type(orig)
- local copy
- if orig_type == 'table' then
- copy = {}
- for orig_key, orig_value in pairs(orig) do
- copy[orig_key] = orig_value
- end
- else
- copy = orig
- end
- return copy
- end
-- Output AST format is the following:
-- table of individual selectors
@@ -570,7 +557,7 @@ exports.parse_selector = function(cfg, str)
return nil
end
- res.selector = shallowcopy(extractors[selector_tbl[1]])
+ res.selector = lua_util.shallowcopy(extractors[selector_tbl[1]])
res.selector.name = selector_tbl[1]
res.selector.args = selector_tbl[2] or {}
@@ -585,7 +572,7 @@ exports.parse_selector = function(cfg, str)
logger.errx(cfg, 'processor %s is unknown', proc_name)
return nil
end
- local processor = shallowcopy(transform_function[proc_name])
+ local processor = lua_util.shallowcopy(transform_function[proc_name])
processor.name = proc_name
processor.args = proc_tbl[2]
lua_util.debugm(M, cfg, 'attached processor %s to selector %s, args: %s',
diff --git a/lualib/lua_util.lua b/lualib/lua_util.lua
index b8e1dbfad..4b18d854c 100644
--- a/lualib/lua_util.lua
+++ b/lualib/lua_util.lua
@@ -688,6 +688,24 @@ end
exports.deepcopy = deepcopy
+--[[[
+-- @function lua_util.shallowcopy(tbl)
+-- Performs shallow (and fast) copy of a table or another Lua type
+--]]
+exports.shallowcopy = function(orig)
+ local orig_type = type(orig)
+ local copy
+ if orig_type == 'table' then
+ copy = {}
+ for orig_key, orig_value in pairs(orig) do
+ copy[orig_key] = orig_value
+ end
+ else
+ copy = orig
+ end
+ return copy
+end
+
-- Debugging support
local unconditional_debug = false
local debug_modules = {}
@@ -721,4 +739,5 @@ exports.debugm = function(mod, ...)
end
end
+
return exports