Skip to content

Commit 01eb53d

Browse files
committed
Added set_keepalive and tests
1 parent 5a27df9 commit 01eb53d

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

lib/resty/redis/connector.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ local DEFAULTS = setmetatable({
8989
connect_timeout = 100,
9090
read_timeout = 1000,
9191
connection_options = {}, -- pool, etc
92+
9293
keepalive_timeout = 60000,
9394
keepalive_poolsize = 30,
9495

@@ -315,4 +316,17 @@ function _M.connect_to_host(self, host)
315316
end
316317

317318

319+
local function set_keepalive(self, redis)
320+
-- Restore connection to "NORMAL" before putting into keepalive pool,
321+
-- ignoring any errors.
322+
redis:discard()
323+
324+
local config = self.config
325+
return redis:set_keepalive(
326+
config.keepalive_timeout, config.keepalive_poolsize
327+
)
328+
end
329+
_M.set_keepalive = set_keepalive
330+
331+
318332
return _M

t/config.t

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,15 @@ location /t {
7070
connect_timeout = 500,
7171
port = 6380,
7272
db = 6,
73+
keepalive_poolsize = 10,
7374
})
7475

7576
assert(config ~= rc.config, "config should not equal rc.config")
7677
assert(rc.config.connect_timeout == 500, "connect_timeout should be 500")
7778
assert(rc.config.db == 6, "db should be 6")
7879
assert(rc.config.role == "master", "role should be master")
80+
assert(rc.config.keepalive_poolsize == 10,
81+
"keepalive_poolsize should be 10")
7982

8083
local redis = assert(rc:connect({ port = 6379 }),
8184
"rc:connect should return positively")

t/connector.t

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
use Test::Nginx::Socket::Lua;
22
use Cwd qw(cwd);
33

4-
repeat_each(2);
5-
6-
plan tests => repeat_each() * (3 * blocks());
4+
plan tests => repeat_each() * (3 * blocks() - 1);
75

86
my $pwd = cwd();
97

@@ -181,3 +179,36 @@ null
181179
an animal
182180
--- no_error_log
183181
[error]
182+
183+
184+
=== TEST 5: Test set_keepalive method
185+
--- http_config eval: $::HttpConfig
186+
--- config
187+
location /t {
188+
lua_socket_log_errors Off;
189+
content_by_lua_block {
190+
local rc = require("resty.redis.connector").new()
191+
192+
local redis = assert(rc:connect(),
193+
"rc:connect should return positively")
194+
local ok, err = rc:set_keepalive(redis)
195+
assert(not err, "set_keepalive error should be nil")
196+
197+
local ok, err = redis:set("foo", "bar")
198+
assert(not ok, "ok should be nil")
199+
assert(string.find(err, "closed"), "error should contain 'closed'")
200+
201+
local redis = assert(rc:connect(), "connect should return positively")
202+
assert(redis:subscribe("channel"), "subscribe should return positively")
203+
204+
local ok, err = rc:set_keepalive(redis)
205+
assert(not ok, "ok should be nil")
206+
assert(string.find(err, "subscribed state"),
207+
"error should contain 'subscribed state'")
208+
209+
}
210+
}
211+
--- request
212+
GET /t
213+
--- no_error_log
214+
[error]

0 commit comments

Comments
 (0)