Skip to content

Commit c452de8

Browse files
committed
accept sentinel options even with string keys
Before, the sentinel options were assumed to have symbol keys, so string keys were ignored. This is surprising, as other Redis options can be indifferently passed as symbols or strings. Now string keys are allowed too.
1 parent f597f21 commit c452de8

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

lib/redis/client.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ class Client
2222
:reconnect_attempts => 1,
2323
:reconnect_delay => 0,
2424
:reconnect_delay_max => 0.5,
25-
:inherit_socket => false
25+
:inherit_socket => false,
26+
:sentinels => nil,
27+
:role => nil
2628
}
2729

2830
attr_reader :options
@@ -89,7 +91,7 @@ def initialize(options = {})
8991
@pending_reads = 0
9092

9193
@connector =
92-
if options.include?(:sentinels)
94+
if !@options[:sentinels].nil?
9395
Connector::Sentinel.new(@options)
9496
elsif options.include?(:connector) && options[:connector].respond_to?(:new)
9597
options.delete(:connector).new(@options)
@@ -539,7 +541,7 @@ def initialize(options)
539541
@options[:db] = DEFAULTS.fetch(:db)
540542

541543
@sentinels = @options.delete(:sentinels).dup
542-
@role = @options.fetch(:role, "master").to_s
544+
@role = (@options[:role] || "master").to_s
543545
@master = @options[:host]
544546
end
545547

@@ -576,10 +578,10 @@ def resolve
576578
def sentinel_detect
577579
@sentinels.each do |sentinel|
578580
client = Client.new(@options.merge({
579-
:host => sentinel[:host],
580-
:port => sentinel[:port],
581-
password: sentinel[:password],
582-
:reconnect_attempts => 0,
581+
host: sentinel[:host] || sentinel["host"],
582+
port: sentinel[:port] || sentinel["port"],
583+
password: sentinel[:password] || sentinel["password"],
584+
reconnect_attempts: 0
583585
}))
584586

585587
begin

test/sentinel_test.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,4 +346,35 @@ def test_sentinel_retries
346346

347347
assert_match(/No sentinels available/, ex.message)
348348
end
349+
350+
def test_sentinel_with_string_option_keys
351+
commands = []
352+
353+
master = {
354+
role: lambda do
355+
['master']
356+
end
357+
}
358+
359+
sentinel = lambda do |port|
360+
{
361+
sentinel: lambda do |command, *args|
362+
commands << [command, *args]
363+
['127.0.0.1', port.to_s]
364+
end
365+
}
366+
end
367+
368+
RedisMock.start(master) do |master_port|
369+
RedisMock.start(sentinel.call(master_port)) do |sen_port|
370+
sentinels = [{ 'host' => '127.0.0.1', 'port' => sen_port }]
371+
372+
redis = Redis.new(url: 'redis://master1', 'sentinels' => sentinels, 'role' => :master)
373+
374+
assert redis.ping
375+
end
376+
end
377+
378+
assert_equal [%w[get-master-addr-by-name master1]], commands
379+
end
349380
end

0 commit comments

Comments
 (0)