Skip to content

Commit 4724c7d

Browse files
committed
Fixed issue where db and password were not passed on when connecting via sentinel
1 parent 241de60 commit 4724c7d

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,10 @@ Attempts to create a connection, according to the [params](#parameters) supplied
140140

141141
### connect_via_sentinel
142142

143-
`syntax: redis, err = rc:connect_via_sentinel(sentinels, master_name, role)`
143+
`syntax: redis, err = rc:connect_via_sentinel(params)`
144144

145-
Returns a Redis connection by first accessing a sentinel as supplied by the `sentinels` table,
146-
and querying this with the `master_name` and `role`.
145+
Returns a Redis connection by first accessing a sentinel as supplied by the `params.sentinels` table,
146+
and querying this with the `params.master_name` and `params.role`.
147147

148148

149149
### try_hosts

lib/resty/redis/connector.lua

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ function _M.connect(self, params)
106106

107107
if params.sentinels then
108108
setmetatable(params, { __index = DEFAULTS } )
109-
return self:connect_via_sentinel(params.sentinels, params.master_name, params.role)
109+
return self:connect_via_sentinel(params)
110110
elseif params.startup_cluster_nodes then
111111
setmetatable(params, { __index = DEFAULTS } )
112112
-- TODO: Implement cluster
@@ -127,7 +127,13 @@ local function sort_by_localhost(a, b)
127127
end
128128

129129

130-
function _M.connect_via_sentinel(self, sentinels, master_name, role)
130+
function _M.connect_via_sentinel(self, params)
131+
local sentinels = params.sentinels
132+
local master_name = params.master_name
133+
local role = params.role
134+
local db = params.db
135+
local password = params.password
136+
131137
local sentnl, err, previous_errors = self:try_hosts(sentinels)
132138
if not sentnl then
133139
return nil, err, previous_errors
@@ -136,6 +142,9 @@ function _M.connect_via_sentinel(self, sentinels, master_name, role)
136142
if role == "master" or role == "any" then
137143
local master, err = sentinel.get_master(sentnl, master_name)
138144
if master then
145+
master.db = db
146+
master.password = password
147+
139148
local redis, err = self:connect_to_host(master)
140149
if redis then
141150
sentnl:set_keepalive()
@@ -159,6 +168,13 @@ function _M.connect_via_sentinel(self, sentinels, master_name, role)
159168
-- Put any slaves on 127.0.0.1 at the front
160169
tbl_sort(slaves, sort_by_localhost)
161170

171+
if db or password then
172+
for i,slave in ipairs(slaves) do
173+
slave.db = db
174+
slave.password = password
175+
end
176+
end
177+
162178
local slave, err, previous_errors = self:try_hosts(slaves)
163179
if not slave then
164180
return nil, err, previous_errors

0 commit comments

Comments
 (0)