Skip to content

Commit af20509

Browse files
committed
Handle unresolved Sentinel maste rerror
redis-rb#178 introduced a regression that caused a ConnectionError to be thrown to the caller if the Sentinel master or replica could not be resolved. When a ConnectionError is thrown, the error message handler would attempt to retrieve `config.server_url`, but this in turn causes another Sentinel resolution to be attempted. We avoid this by adding a `resolved?` method that will indicate whether the config can be used. The error handler won't attempt to provide more details if the config has yet to be resolved. Closes redis-rb#182
1 parent 93c343a commit af20509

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

lib/redis_client.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def _set_config(config)
8585
end
8686

8787
def message
88-
return super unless config
88+
return super unless config&.resolved?
8989

9090
"#{super} (#{config.server_url})"
9191
end

lib/redis_client/config.rb

+4
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ def username
9191
@username || DEFAULT_USERNAME
9292
end
9393

94+
def resolved?
95+
true
96+
end
97+
9498
def sentinel?
9599
false
96100
end

lib/redis_client/sentinel_config.rb

+6
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ def check_role!(role)
112112
end
113113
end
114114

115+
def resolved?
116+
@mutex.synchronize do
117+
!!@config
118+
end
119+
end
120+
115121
private
116122

117123
def sentinels_to_configs(sentinels)

test/sentinel/sentinel_test.rb

+16
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,22 @@ def test_unknown_master
8383
end
8484
end
8585

86+
def test_unresolved_config
87+
client = @config.new_client
88+
89+
@config.stub(:server_url, -> { raise ConnectionError.with_config('this should not be called', @config) }) do
90+
@config.stub(:resolved?, false) do
91+
client.stub(:call, ->(_) { raise ConnectionError.with_config('call error', @config) }) do
92+
error = assert_raises ConnectionError do
93+
client.call('PING')
94+
end
95+
96+
assert_equal 'call error', error.message
97+
end
98+
end
99+
end
100+
end
101+
86102
def test_master_failover_not_ready
87103
sentinel_client_mock = SentinelClientMock.new([
88104
[["SENTINEL", "get-master-addr-by-name", "cache"], [Servers::REDIS_REPLICA.host, Servers::REDIS_REPLICA.port.to_s]],

0 commit comments

Comments
 (0)