|
1 |
| -local ipairs, pcall, error, tostring, type, next, setmetatable, getmetatable = |
2 |
| - ipairs, pcall, error, tostring, type, next, setmetatable, getmetatable |
| 1 | +local ipairs, pairs, pcall, error, tostring, type, next, setmetatable, getmetatable = |
| 2 | + ipairs, pairs, pcall, error, tostring, type, next, setmetatable, getmetatable |
3 | 3 |
|
4 | 4 | local ngx_log = ngx.log
|
5 | 5 | local ngx_ERR = ngx.ERR
|
6 | 6 | local ngx_re_match = ngx.re.match
|
| 7 | +local null = ngx.null |
7 | 8 |
|
8 | 9 | local str_find = string.find
|
9 | 10 | local str_sub = string.sub
|
10 | 11 | local tbl_remove = table.remove
|
11 | 12 | local tbl_sort = table.sort
|
12 |
| -local ok, tbl_new = pcall(require, "table.new") |
13 |
| -if not ok then |
14 |
| - tbl_new = function (narr, nrec) return {} end -- luacheck: ignore 212 |
| 13 | +local tbl_clone, tbl_new |
| 14 | +do |
| 15 | + local ok |
| 16 | + ok, tbl_new = pcall(require, "table.new") |
| 17 | + if not ok then |
| 18 | + tbl_new = function (narr, nrec) return {} end -- luacheck: ignore 212 |
| 19 | + end |
| 20 | + ok, tbl_clone = pcall(require, "table.clone") |
| 21 | + if not ok then |
| 22 | + tbl_clone = function (src) |
| 23 | + local result = {} |
| 24 | + for i, v in ipairs(src) do |
| 25 | + result[i] = v |
| 26 | + end |
| 27 | + for k, v in pairs(src) do |
| 28 | + result[k] = v |
| 29 | + end |
| 30 | + |
| 31 | + return result |
| 32 | + end |
| 33 | + end |
15 | 34 | end
|
16 | 35 |
|
17 | 36 | local redis = require("resty.redis")
|
@@ -253,19 +272,19 @@ end
|
253 | 272 |
|
254 | 273 |
|
255 | 274 | function _M.connect_via_sentinel(self, params)
|
256 |
| - local sentinels = params.sentinels |
257 | 275 | local master_name = params.master_name
|
258 | 276 | local role = params.role
|
259 | 277 | local db = params.db
|
260 | 278 | local username = params.username
|
261 | 279 | local password = params.password
|
262 |
| - local sentinel_username = params.sentinel_username |
263 |
| - local sentinel_password = params.sentinel_password |
264 |
| - if sentinel_password then |
265 |
| - for _, host in ipairs(sentinels) do |
266 |
| - host.username = sentinel_username |
267 |
| - host.password = sentinel_password |
268 |
| - end |
| 280 | + |
| 281 | + local sentinels = tbl_new(#params.sentinels, 0) |
| 282 | + for i, sentinel in ipairs(params.sentinels) do |
| 283 | + local host = tbl_clone(sentinel) |
| 284 | + host.db = null |
| 285 | + host.username = host.username or params.sentinel_username |
| 286 | + host.password = host.password or params.sentinel_password |
| 287 | + sentinels[i] = host |
269 | 288 | end
|
270 | 289 |
|
271 | 290 | local sentnl, err, previous_errors = self:try_hosts(sentinels)
|
@@ -344,7 +363,7 @@ function _M.connect_to_host(self, host)
|
344 | 363 |
|
345 | 364 | -- config options in 'host' should override the global defaults
|
346 | 365 | -- host contains keys that aren't in config
|
347 |
| - -- this can break tbl_copy_merge_defaults, hence the mannual loop here |
| 366 | + -- this can break tbl_copy_merge_defaults, hence the manual loop here |
348 | 367 | local config = tbl_copy(self.config)
|
349 | 368 | for k, _ in pairs(config) do
|
350 | 369 | if host[k] then
|
@@ -402,20 +421,20 @@ function _M.connect_to_host(self, host)
|
402 | 421 | end
|
403 | 422 | end
|
404 | 423 |
|
405 |
| - -- No support for DBs in proxied Redis. |
406 |
| - if config.connection_is_proxied ~= true and host.db ~= nil then |
407 |
| - local res, err = r:select(host.db) |
| 424 | + -- No support for DBs in proxied Redis and Redis Sentinel |
| 425 | + if config.connection_is_proxied ~= true and host.db ~= nil and host.db ~= null then |
| 426 | + local res, select_err = r:select(host.db) |
408 | 427 |
|
409 | 428 | -- SELECT will fail if we are connected to sentinel:
|
410 | 429 | -- detect it and ignore error message it that's the case
|
411 |
| - if err and str_find(err, "ERR unknown command") then |
| 430 | + if select_err and str_find(select_err, "ERR unknown command") then |
412 | 431 | local role = r:role()
|
413 | 432 | if role and role[1] == "sentinel" then
|
414 |
| - err = nil |
| 433 | + select_err = nil |
415 | 434 | end
|
416 | 435 | end
|
417 |
| - if err then |
418 |
| - return res, err |
| 436 | + if select_err then |
| 437 | + return res, select_err |
419 | 438 | end
|
420 | 439 | end
|
421 | 440 | return r, nil
|
|
0 commit comments