From 67dbbf55c790a64e97ff9818eaa9a18aeb9f3d4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=89=E6=9C=8B?= <22249030@qq.com> Date: Mon, 5 May 2025 23:13:01 +0800 Subject: [PATCH 1/2] bugfix: connection is closed after the blpop and brpop calls time out These two calls belong to blocking calls, and the socket should not be closed after timeout --- lib/resty/redis.lua | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/resty/redis.lua b/lib/resty/redis.lua index a7f3ec6..b0c72f9 100644 --- a/lib/resty/redis.lua +++ b/lib/resty/redis.lua @@ -52,6 +52,9 @@ local sub_commands = { "subscribe", "psubscribe" } +local blocking_commands = { + "blpop", "brpop" +} local unsub_commands = { "unsubscribe", "punsubscribe" @@ -243,7 +246,7 @@ _M.close = close local function _read_reply(self, sock) local line, err = sock:receive() if not line then - if err == "timeout" and not rawget(self, "_subscribed") then + if err == "timeout" and not rawget(self, "_subscribed") and not rawget(self, "_blocking") then sock:close() end return nil, err @@ -502,6 +505,17 @@ for i = 1, #common_cmds do end end +for i = 1, #blocking_commands do + local cmd = blocking_commands[i] + + _M[cmd] = + function (self, ...) + if not rawget(self, "_blocking") then + self._blocking = true + end + return do_cmd(self, cmd, ...) + end +end local function handle_subscribe_result(self, cmd, nargs, res) local err From c364f7ef5b28a86fa50b6aaca68ccb416159a01a Mon Sep 17 00:00:00 2001 From: lijunlong Date: Tue, 6 May 2025 22:01:03 +0800 Subject: [PATCH 2/2] update tests. --- t/count.t | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/count.t b/t/count.t index 91b6c40..18514bd 100644 --- a/t/count.t +++ b/t/count.t @@ -22,6 +22,6 @@ __DATA__ ngx.say("size: ", n) '; --- response_body -size: 56 +size: 58 --- no_error_log [error]