Skip to content

Commit 132158c

Browse files
committed
Have sentinel honor reconnect_attempts option.
1 parent 5c6d9a1 commit 132158c

File tree

2 files changed

+72
-20
lines changed

2 files changed

+72
-20
lines changed

lib/redis/client.rb

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -520,28 +520,25 @@ def resolve
520520
end
521521

522522
def sentinel_detect
523-
3.times do
524-
@sentinels.each do |sentinel|
525-
client = Client.new(@options.merge({
526-
:host => sentinel[:host],
527-
:port => sentinel[:port]
528-
}))
529-
530-
begin
531-
if result = yield(client)
532-
# This sentinel responded. Make sure we ask it first next time.
533-
@sentinels.delete(sentinel)
534-
@sentinels.unshift(sentinel)
535-
536-
return result
537-
end
538-
rescue CannotConnectError
539-
ensure
540-
client.disconnect
523+
@sentinels.each do |sentinel|
524+
client = Client.new(@options.merge({
525+
:host => sentinel[:host],
526+
:port => sentinel[:port],
527+
:reconnect_attempts => 0,
528+
}))
529+
530+
begin
531+
if result = yield(client)
532+
# This sentinel responded. Make sure we ask it first next time.
533+
@sentinels.delete(sentinel)
534+
@sentinels.unshift(sentinel)
535+
536+
return result
541537
end
538+
rescue BaseConnectionError
539+
ensure
540+
client.disconnect
542541
end
543-
544-
sleep(0.3)
545542
end
546543

547544
raise CannotConnectError, "No sentinels available."

test/sentinel_test.rb

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,59 @@ def test_sentinel_role_mismatch
175175

176176
assert_match /Instance role mismatch/, ex.message
177177
end
178+
179+
def test_sentinel_retries
180+
sentinels = [{:host => "127.0.0.1", :port => 26381},
181+
{:host => "127.0.0.1", :port => 26382}]
182+
183+
connections = []
184+
185+
handler = lambda do |id|
186+
{
187+
:sentinel => lambda do |command, *args|
188+
connections << id
189+
190+
if connections.count(id) < 2
191+
:close
192+
else
193+
["127.0.0.1", "6382"]
194+
end
195+
end
196+
}
197+
end
198+
199+
master = {
200+
:role => lambda do
201+
["master"]
202+
end
203+
}
204+
205+
RedisMock.start(master, {}, 6382) do
206+
RedisMock.start(handler.call(:s1), {}, 26381) do
207+
RedisMock.start(handler.call(:s2), {}, 26382) do
208+
redis = Redis.new(:url => "redis://master1", :sentinels => sentinels, :role => :master, :reconnect_attempts => 1)
209+
210+
assert redis.ping
211+
end
212+
end
213+
end
214+
215+
assert_equal [:s1, :s2, :s1], connections
216+
217+
connections.clear
218+
219+
ex = assert_raise(Redis::CannotConnectError) do
220+
RedisMock.start(master, {}, 6382) do
221+
RedisMock.start(handler.call(:s1), {}, 26381) do
222+
RedisMock.start(handler.call(:s2), {}, 26382) do
223+
redis = Redis.new(:url => "redis://master1", :sentinels => sentinels, :role => :master, :reconnect_attempts => 0)
224+
225+
assert redis.ping
226+
end
227+
end
228+
end
229+
end
230+
231+
assert_match /No sentinels available/, ex.message
232+
end
178233
end

0 commit comments

Comments
 (0)