aboutsummaryrefslogtreecommitdiffstats
path: root/lualib
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2018-02-12 12:15:55 +0000
committerVsevolod Stakhov <vsevolod@highsecure.ru>2018-02-12 15:57:33 +0000
commit7305bc1e97ce5c81c7226fdfda307213335cff2f (patch)
tree15d4cb7806c24daab19c75992089bf6ab00175c0 /lualib
parentf608f7d32a024cd9d8f36cd26cb0fcc745842b4e (diff)
downloadrspamd-7305bc1e97ce5c81c7226fdfda307213335cff2f.tar.gz
rspamd-7305bc1e97ce5c81c7226fdfda307213335cff2f.zip
[Feature] Add method to do a synchronous Redis connection
Diffstat (limited to 'lualib')
-rw-r--r--lualib/lua_redis.lua55
1 files changed, 55 insertions, 0 deletions
diff --git a/lualib/lua_redis.lua b/lualib/lua_redis.lua
index 49b3136e7..1e53c6b6c 100644
--- a/lualib/lua_redis.lua
+++ b/lualib/lua_redis.lua
@@ -835,4 +835,59 @@ end
exports.exec_redis_script = exec_redis_script
+local function redis_connect_sync(redis_params, is_write, key, cfg)
+ if not redis_params then
+ return false,nil
+ end
+
+ local rspamd_redis = require "rspamd_redis"
+ 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(cfg, 'cannot select server to make redis request')
+ end
+
+ local options = {
+ host = addr:get_addr(),
+ timeout = redis_params['timeout'],
+ }
+
+
+ local ret,conn = rspamd_redis.connect_sync(options)
+ if not ret then
+ logger.errx('cannot execute redis request: %s', conn)
+ addr:fail()
+ end
+
+ if conn then
+ if redis_params['password'] then
+ conn:add_cmd('AUTH', {redis_params['password']})
+ end
+
+ if redis_params['db'] then
+ conn:add_cmd('SELECT', {tostring(redis_params['db'])})
+ elseif redis_params['dbname'] then
+ conn:add_cmd('SELECT', {tostring(redis_params['dbname'])})
+ end
+ end
+
+ return ret,conn,addr
+end
+
+exports.redis_connect_sync = redis_connect_sync
+
return exports