Skip to content

Commit 4086ec9

Browse files
committed
try_hosts err should reflect ability to connect
previously err was set to last err, now it will be nil if one host connected and previous_errors contains all errors
1 parent a388dac commit 4086ec9

File tree

2 files changed

+43
-51
lines changed

2 files changed

+43
-51
lines changed

lib/resty/redis/connector.lua

+7-6
Original file line numberDiff line numberDiff line change
@@ -262,13 +262,15 @@ function _M.try_hosts(self, hosts)
262262
local errors = tbl_new(#hosts, 0)
263263

264264
for i, host in ipairs(hosts) do
265-
local r
266-
r, errors[i] = self:connect_to_host(host)
267-
if r then
268-
return r, tbl_remove(errors), errors
265+
local r, err = self:connect_to_host(host)
266+
if r and not err then
267+
return r, nil, errors
268+
else
269+
errors[i] = err
269270
end
270271
end
271-
return nil, tbl_remove(errors), errors
272+
273+
return nil, "no hosts available", errors
272274
end
273275

274276

@@ -295,7 +297,6 @@ function _M.connect_to_host(self, host)
295297
end
296298

297299
if not ok then
298-
ngx_log(ngx_ERR, err, " for ", host.host, ":", host.port)
299300
return nil, err
300301
else
301302
r:set_timeout(self, config.read_timeout)

t/connector.t

+36-45
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
use Test::Nginx::Socket::Lua;
1+
use Test::Nginx::Socket 'no_plan';
22
use Cwd qw(cwd);
33

4-
plan tests => repeat_each() * (3 * blocks() - 1);
5-
64
my $pwd = cwd();
75

86
our $HttpConfig = qq{
@@ -17,48 +15,39 @@ run_tests();
1715

1816
__DATA__
1917

20-
=== TEST 1: basic
18+
=== TEST 1: basic connect
2119
--- http_config eval: $::HttpConfig
2220
--- config
2321
location /t {
2422
content_by_lua_block {
25-
local rc = require("resty.redis.connector").new()
26-
27-
local params = { host = "127.0.0.1", port = $TEST_NGINX_REDIS_PORT }
28-
29-
local redis, err = rc:connect(params)
30-
if not redis then
31-
ngx.say("failed to connect: ", err)
32-
return
33-
end
23+
local rc = require("resty.redis.connector").new({
24+
port = $TEST_NGINX_REDIS_PORT
25+
})
3426

35-
local res, err = redis:set("dog", "an animal")
36-
if not res then
37-
ngx.say("failed to set dog: ", err)
38-
return
39-
end
27+
local redis = assert(rc:connect(params),
28+
"connect should return positively")
4029

41-
ngx.say("set dog: ", res)
30+
assert(redis:set("dog", "an animal"),
31+
"redis:set should return positively")
4232

4333
redis:close()
4434
}
4535
}
4636
--- request
4737
GET /t
48-
--- response_body
49-
set dog: OK
5038
--- no_error_log
5139
[error]
5240

5341

54-
=== TEST 2: test we can try a list of hosts, and connect to the first working one
42+
=== TEST 2: try_hosts
5543
--- http_config eval: $::HttpConfig
5644
--- config
5745
location /t {
46+
lua_socket_log_errors off;
5847
content_by_lua_block {
59-
local redis_connector = require "resty.redis.connector"
60-
local rc = redis_connector.new()
61-
rc:set_connect_timeout(100)
48+
local rc = require("resty.redis.connector").new({
49+
connect_timeout = 100,
50+
})
6251

6352
local hosts = {
6453
{ host = "127.0.0.1", port = 1 },
@@ -67,36 +56,38 @@ location /t {
6756
}
6857

6958
local redis, err, previous_errors = rc:try_hosts(hosts)
70-
if not redis then
71-
ngx.say("failed to connect: ", err)
72-
return
73-
end
59+
assert(redis and not err,
60+
"try_hosts should return a connection and no error")
7461

75-
-- Print the failed connection errors
76-
ngx.say("connection 1 error: ", err)
62+
assert(previous_errors[1] == "connection refused",
63+
"previous_errors[1] should be 'connection refused'")
64+
assert(previous_errors[2] == "connection refused",
65+
"previous_errors[2] should be 'connection refused'")
7766

78-
ngx.say("connection 2 error: ", previous_errors[1])
67+
assert(redis:set("dog", "an animal"),
68+
"redis connection should be working")
7969

80-
local res, err = redis:set("dog", "an animal")
81-
if not res then
82-
ngx.say("failed to set dog: ", err)
83-
return
84-
end
70+
redis:close()
8571

86-
ngx.say("set dog: ", res)
72+
local hosts = {
73+
{ host = "127.0.0.1", port = 1 },
74+
{ host = "127.0.0.1", port = 2 },
75+
}
8776

77+
local redis, err, previous_errors = rc:try_hosts(hosts)
78+
assert(not redis and err == "no hosts available",
79+
"no available hosts should return an error")
8880

89-
redis:close()
81+
assert(previous_errors[1] == "connection refused",
82+
"previous_errors[1] should be 'connection refused'")
83+
assert(previous_errors[2] == "connection refused",
84+
"previous_errors[2] should be 'connection refused'")
9085
}
9186
}
9287
--- request
9388
GET /t
94-
--- response_body
95-
connection 1 error: connection refused
96-
connection 2 error: connection refused
97-
set dog: OK
98-
--- error_log
99-
111: Connection refused
89+
--- no_error_log
90+
[error]
10091

10192

10293
=== TEST 3: Test connect_to_host directly

0 commit comments

Comments
 (0)