From 21001514e8adf0d6d6e767c9a0d59ef0f6dc7596 Mon Sep 17 00:00:00 2001 From: fankeke <382482175@qq.com> Date: Mon, 3 Oct 2016 09:39:36 +0800 Subject: [PATCH 1/3] Update rediscluster.lua set `local` to redis client --- lib/resty/rediscluster.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/resty/rediscluster.lua b/lib/resty/rediscluster.lua index 7a3d30a..32c959a 100644 --- a/lib/resty/rediscluster.lua +++ b/lib/resty/rediscluster.lua @@ -209,7 +209,7 @@ local function _do_cmd(self, cmd, key, ...) for i=1,#serv_list do local ip = serv_list[index].ip local port = serv_list[index].port - redis_client = redis:new() + local redis_client = redis:new() local ok, err = redis_client:connect(ip_string(ip), port) if ok then slots[slot].cur = index From 218fe1fa30cd2d8eabc2acd615c744aa715e85d9 Mon Sep 17 00:00:00 2001 From: fankeke <382482175@qq.com> Date: Wed, 5 Apr 2017 20:43:52 -0700 Subject: [PATCH 2/3] set timeout for redis --- lib/resty/rediscluster.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/resty/rediscluster.lua b/lib/resty/rediscluster.lua index 32c959a..e926993 100644 --- a/lib/resty/rediscluster.lua +++ b/lib/resty/rediscluster.lua @@ -210,6 +210,7 @@ local function _do_cmd(self, cmd, key, ...) local ip = serv_list[index].ip local port = serv_list[index].port local redis_client = redis:new() + redis_client:set_timeout(config.timeout or 1000) --in ms local ok, err = redis_client:connect(ip_string(ip), port) if ok then slots[slot].cur = index From fb25d2a418da4a83eea86b2020a5f6291ba2db92 Mon Sep 17 00:00:00 2001 From: fankeke <382482175@qq.com> Date: Wed, 5 Apr 2017 23:56:04 -0700 Subject: [PATCH 3/3] add connectimeout and reuse redis instance in one request --- lib/resty/rediscluster.lua | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/resty/rediscluster.lua b/lib/resty/rediscluster.lua index e926993..96e0c52 100644 --- a/lib/resty/rediscluster.lua +++ b/lib/resty/rediscluster.lua @@ -189,6 +189,7 @@ end local MAGIC_TRY = 3 local DEFUALT_KEEPALIVE_TIMEOUT = 1000 local DEFAULT_KEEPALIVE_CONS = 200 +local DEFAULT_CONNECT_TIMEOUT = 10 local function _do_cmd(self, cmd, key, ...) if self._reqs then @@ -201,6 +202,8 @@ local function _do_cmd(self, cmd, key, ...) key = tostring(key) local slot = redis_slot(key) + local redis_client = redis:new() + redis_client:set_timeout(config.connect_timeout or DEFAULT_CONNECT_TIMEOUT) for k=1, MAGIC_TRY do local slots = slot_cache[self.config.name] @@ -209,18 +212,16 @@ local function _do_cmd(self, cmd, key, ...) for i=1,#serv_list do local ip = serv_list[index].ip local port = serv_list[index].port - local redis_client = redis:new() - redis_client:set_timeout(config.timeout or 1000) --in ms local ok, err = redis_client:connect(ip_string(ip), port) if ok then slots[slot].cur = index local res, err = redis_client[cmd](redis_client, key, ...) - redis_client:set_keepalive(config.keepalive_timeout or DEFUALT_KEEPALIVE_TIMEOUT, - config.keepalove_cons or DEFAULT_KEEPALIVE_CONS) if err and string.sub(err, 1, 5) == "MOVED" then self:fetch_slots() break end + redis_client:set_keepalive(config.keepalive_timeout or DEFUALT_KEEPALIVE_TIMEOUT, + config.keepalove_cons or DEFAULT_KEEPALIVE_CONS) return res, err else index = next_index(index, #serv_list)