Skip to content

Commit fbcbfc1

Browse files
committed
Merge branch 'zmack-fix_unconnectable_sentinel'
Closes #494.
2 parents 58ff0c2 + 132158c commit fbcbfc1

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

lib/redis/client.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,8 @@ def sentinel_detect
530530
@sentinels.each do |sentinel|
531531
client = Client.new(@options.merge({
532532
:host => sentinel[:host],
533-
:port => sentinel[:port]
533+
:port => sentinel[:port],
534+
:reconnect_attempts => 0,
534535
}))
535536

536537
begin
@@ -541,12 +542,13 @@ def sentinel_detect
541542

542543
return result
543544
end
545+
rescue BaseConnectionError
544546
ensure
545547
client.disconnect
546548
end
547549
end
548550

549-
return nil
551+
raise CannotConnectError, "No sentinels available."
550552
end
551553

552554
def resolve_master

test/sentinel_test.rb

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

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

0 commit comments

Comments
 (0)