File tree 1 file changed +15
-0
lines changed
1 file changed +15
-0
lines changed Original file line number Diff line number Diff line change @@ -39,6 +39,7 @@ redis.add_commands("sentinel")
39
39
local get_master = require (" resty.redis.sentinel" ).get_master
40
40
local get_slaves = require (" resty.redis.sentinel" ).get_slaves
41
41
42
+ local slave_rr_counter = 0
42
43
43
44
-- A metatable which prevents undefined fields from being created / accessed
44
45
local fixed_field_metatable = {
@@ -262,6 +263,19 @@ function _M.connect(self, params)
262
263
end
263
264
264
265
266
+ local function rotate_slaves (slaves )
267
+ local rotated_slaves = {}
268
+ local local_rr_counter = (slave_rr_counter + 1 ) % # slaves
269
+ slave_rr_counter = local_rr_counter
270
+
271
+ for i , _ in ipairs (slaves ) do
272
+ local_rr_counter = (local_rr_counter % # slaves ) + 1
273
+ rotated_slaves [i ] = slaves [local_rr_counter ]
274
+ end
275
+
276
+ return rotated_slaves
277
+ end
278
+
265
279
local function sort_by_localhost (a , b )
266
280
if a .host == " 127.0.0.1" and b .host ~= " 127.0.0.1" then
267
281
return true
@@ -319,6 +333,7 @@ function _M.connect_via_sentinel(self, params)
319
333
320
334
sentnl :set_keepalive ()
321
335
336
+ slaves = rotate_slaves (slaves )
322
337
-- Put any slaves on 127.0.0.1 at the front
323
338
tbl_sort (slaves , sort_by_localhost )
324
339
You can’t perform that action at this time.
0 commit comments