Skip to content

Commit f7dfa6b

Browse files
authored
Merge pull request #1210 from casperisfine/redis-client-0.15
Fix compatibility with `redis-client 0.15.0` when using Redis Sentinel
2 parents f21cbca + ef8817e commit f7dfa6b

File tree

5 files changed

+35
-27
lines changed

5 files changed

+35
-27
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Unreleased
22

3+
- Fix compatibility with `redis-client 0.15.0` when using Redis Sentinel. Fix #1209.
4+
35
# 5.0.6
46

57
- Wait for an extra `config.read_timeout` in blocking commands rather than an arbitrary 100ms. See #1175.

lib/redis.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@ def send_command(command, &block)
166166
@monitor.synchronize do
167167
@client.call_v(command, &block)
168168
end
169+
rescue ::RedisClient::Error => error
170+
Client.translate_error!(error)
169171
end
170172

171173
def send_blocking_command(command, timeout, &block)

lib/redis/client.rb

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,24 @@ def config(**kwargs)
2424
end
2525

2626
def sentinel(**kwargs)
27-
super(protocol: 2, **kwargs)
27+
super(protocol: 2, **kwargs, client_implementation: ::RedisClient)
28+
end
29+
30+
def translate_error!(error)
31+
redis_error = translate_error_class(error.class)
32+
raise redis_error, error.message, error.backtrace
33+
end
34+
35+
private
36+
37+
def translate_error_class(error_class)
38+
ERROR_MAPPING.fetch(error_class)
39+
rescue IndexError
40+
if (client_error = error_class.ancestors.find { |a| ERROR_MAPPING[a] })
41+
ERROR_MAPPING[error_class] = ERROR_MAPPING[client_error]
42+
else
43+
raise
44+
end
2845
end
2946
end
3047

@@ -72,7 +89,7 @@ def password
7289
def call_v(command, &block)
7390
super(command, &block)
7491
rescue ::RedisClient::Error => error
75-
translate_error!(error)
92+
Client.translate_error!(error)
7693
end
7794

7895
def blocking_call_v(timeout, command, &block)
@@ -85,19 +102,19 @@ def blocking_call_v(timeout, command, &block)
85102

86103
super(timeout, command, &block)
87104
rescue ::RedisClient::Error => error
88-
translate_error!(error)
105+
Client.translate_error!(error)
89106
end
90107

91108
def pipelined
92109
super
93110
rescue ::RedisClient::Error => error
94-
translate_error!(error)
111+
Client.translate_error!(error)
95112
end
96113

97114
def multi
98115
super
99116
rescue ::RedisClient::Error => error
100-
translate_error!(error)
117+
Client.translate_error!(error)
101118
end
102119

103120
def disable_reconnection(&block)
@@ -107,22 +124,5 @@ def disable_reconnection(&block)
107124
def inherit_socket!
108125
@inherit_socket = true
109126
end
110-
111-
private
112-
113-
def translate_error!(error)
114-
redis_error = translate_error_class(error.class)
115-
raise redis_error, error.message, error.backtrace
116-
end
117-
118-
def translate_error_class(error_class)
119-
ERROR_MAPPING.fetch(error_class)
120-
rescue IndexError
121-
if (client_error = error_class.ancestors.find { |a| ERROR_MAPPING[a] })
122-
ERROR_MAPPING[error_class] = ERROR_MAPPING[client_error]
123-
else
124-
raise
125-
end
126-
end
127127
end
128128
end

test/redis/client_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ def test_call_raise
2828

2929
def test_error_translate_subclasses
3030
error = Class.new(RedisClient::CommandError)
31-
assert_equal Redis::CommandError, r._client.send(:translate_error_class, error)
31+
assert_equal Redis::CommandError, Redis::Client.send(:translate_error_class, error)
3232

3333
assert_raises KeyError do
34-
r._client.send(:translate_error_class, StandardError)
34+
Redis::Client.send(:translate_error_class, StandardError)
3535
end
3636
end
3737

test/sentinel/sentinel_test.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ def test_sentinel_with_non_sentinel_options
197197
end
198198
end
199199

200-
assert_equal [%w[get-master-addr-by-name master1], ["sentinels", "master1"]], commands[:s1]
200+
assert_equal [%w[auth foo], %w[get-master-addr-by-name master1], ["sentinels", "master1"]], commands[:s1]
201201
assert_equal [%w[auth foo], %w[role]], commands[:m1]
202202
end
203203

@@ -409,12 +409,15 @@ def test_sentinel_retries
409409

410410
connections = []
411411

412+
fails = Hash.new(0)
413+
412414
handler = lambda do |id, port|
413415
{
414416
sentinel: lambda do |command, *_args|
415417
connections << id
416418

417-
if connections.count(id) < 2
419+
if fails[id] < 2
420+
fails[id] += 1
418421
:close
419422
else
420423
case command
@@ -451,9 +454,10 @@ def test_sentinel_retries
451454
end
452455
end
453456

454-
assert_equal %i[s1 s2 s1 s1], connections
457+
assert_equal %i[s1 s1 s2 s2 s1 s1], connections
455458

456459
connections.clear
460+
fails.clear
457461

458462
ex = assert_raises(Redis::CannotConnectError) do
459463
RedisMock.start(master) do |master_port|

0 commit comments

Comments
 (0)