Skip to content

Commit 9d53749

Browse files
piotrppintsized
authored andcommitted
Return error when connecting to invalid master_name
Previously we returned nil connection with no errors. This also makes get_master behave similar to get_slaves in absence of eligible Redis instances, i.e. it returns an error.
1 parent ee6e39a commit 9d53749

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

lib/resty/redis/sentinel.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ function _M.get_master(sentinel, master_name)
2121
)
2222
if res and res ~= ngx_null and res[1] and res[2] then
2323
return { host = res[1], port = res[2] }
24+
elseif res == ngx_null then
25+
return nil, "invalid master name"
2426
else
2527
return nil, err
2628
end

t/sentinel.t

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,22 @@ __DATA__
3131
location /t {
3232
content_by_lua_block {
3333
local rc = require("resty.redis.connector").new()
34+
local rs = require("resty.redis.sentinel")
3435
3536
local sentinel, err = rc:connect{ url = "redis://127.0.0.1:$TEST_NGINX_SENTINEL_PORT1" }
3637
assert(sentinel and not err, "sentinel should connect without errors but got " .. tostring(err))
3738
38-
local master, err = require("resty.redis.sentinel").get_master(
39-
sentinel,
40-
"mymaster"
41-
)
39+
local master, err = rs.get_master(sentinel, "mymaster")
4240
4341
assert(master and not err, "get_master should return the master")
4442
4543
assert(master.host == "127.0.0.1" and tonumber(master.port) == $TEST_NGINX_REDIS_PORT,
4644
"host should be 127.0.0.1 and port should be $TEST_NGINX_REDIS_PORT")
4745
46+
master, err = rs.get_master(sentinel, "invalid-mymaster")
47+
48+
assert(not master and err, "invalid master name should result in error")
49+
4850
sentinel:close()
4951
}
5052
}
@@ -87,14 +89,12 @@ GET /t
8789
location /t {
8890
content_by_lua_block {
8991
local rc = require("resty.redis.connector").new()
92+
local rs = require("resty.redis.sentinel")
9093
9194
local sentinel, err = rc:connect{ url = "redis://127.0.0.1:$TEST_NGINX_SENTINEL_PORT1" }
9295
assert(sentinel and not err, "sentinel should connect without error")
9396
94-
local slaves, err = require("resty.redis.sentinel").get_slaves(
95-
sentinel,
96-
"mymaster"
97-
)
97+
local slaves, err = rs.get_slaves(sentinel, "mymaster")
9898
9999
assert(slaves and not err, "slaves should be returned without error")
100100
@@ -107,6 +107,10 @@ location /t {
107107
assert(slaveports["$TEST_NGINX_REDIS_PORT_SL1"] == true and slaveports["$TEST_NGINX_REDIS_PORT_SL2"] == true,
108108
"slaves should both be found")
109109
110+
slaves, err = rs.get_slaves(sentinel, "invalid-mymaster")
111+
112+
assert(not slaves and err, "invalid master name should result in error")
113+
110114
sentinel:close()
111115
}
112116
}

0 commit comments

Comments
 (0)