diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2016-06-22 14:38:16 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2016-06-22 14:38:16 +0100 |
commit | b84d1f706247c4ff3ec0bf86e6c8af1ab5537642 (patch) | |
tree | 0d592f25608d242141b8faf3eee72b25d6dde5ad | |
parent | c2392de111b13a03a9c7c33bf56fb0949c0d31e3 (diff) | |
download | rspamd-b84d1f706247c4ff3ec0bf86e6c8af1ab5537642.tar.gz rspamd-b84d1f706247c4ff3ec0bf86e6c8af1ab5537642.zip |
[Feature] Add universal function to make a proper redis request
-rw-r--r-- | src/lua/global_functions.lua | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/lua/global_functions.lua b/src/lua/global_functions.lua index 0aacd3989..b68c5d582 100644 --- a/src/lua/global_functions.lua +++ b/src/lua/global_functions.lua @@ -47,6 +47,8 @@ function rspamd_parse_redis_server(module_name) end if options['db'] then ret['db'] = options['db'] + elseif options['dbname'] then + ret['db'] = options['dbname'] end if options['password'] then ret['password'] = options['password'] @@ -89,6 +91,59 @@ function rspamd_parse_redis_server(module_name) return ret end +-- Performs async call to redis hiding all complexity inside function +-- task - rspamd_task +-- redis_params - valid params returned by rspamd_parse_redis_server +-- key - key to select upstream or nil to select round-robin/master-slave +-- is_write - true if need to write to redis server +-- callback - function to be called upon request is completed +-- command - redis command +-- args - table of arguments +function rspamd_redis_make_request(task, redis_params, key, is_write, callback, command, args) + if not task or not redis_params or not callback or not command then + return false,nil,nil + end + + local addr + + 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 + logger.errx(task, 'cannot select server to make redis request') + end + + local options = { + task = task, + 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 + + return rspamd_redis.make_request(options),addr +end + function rspamd_str_split(s, sep) local lpeg = require "lpeg" sep = lpeg.P(sep) |