Skip to content

Commit ddf058b

Browse files
authored
Merge pull request #591 from tgxworld/allow_custom_connectors
Add option to use a different Connector.
2 parents bc80cb1 + 088b8c6 commit ddf058b

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

lib/redis.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def self.current=(redis)
3131
# @option options [Boolean] :inherit_socket (false) Whether to use socket in forked process or not
3232
# @option options [Array] :sentinels List of sentinels to contact
3333
# @option options [Symbol] :role (:master) Role to fetch via Sentinel, either `:master` or `:slave`
34+
# @option options [Class] :connector Class of custom connector
3435
#
3536
# @return [Redis] a new client instance
3637
def initialize(options = {})

lib/redis/client.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,14 @@ def initialize(options = {})
8484

8585
@pending_reads = 0
8686

87-
if options.include?(:sentinels)
88-
@connector = Connector::Sentinel.new(@options)
89-
else
90-
@connector = Connector.new(@options)
91-
end
87+
@connector =
88+
if options.include?(:sentinels)
89+
Connector::Sentinel.new(@options)
90+
elsif options.include?(:connector) && options[:connector].respond_to?(:new)
91+
options.delete(:connector).new(@options)
92+
else
93+
Connector.new(@options)
94+
end
9295
end
9396

9497
def connect

test/client_test.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,21 @@ def test_queue_after_error
5656

5757
assert_equal result, ["OK", 1]
5858
end
59+
60+
def test_client_with_custom_connector
61+
custom_connector = Class.new(Redis::Client::Connector) do
62+
def resolve
63+
@options[:host] = '127.0.0.5'
64+
@options[:port] = '999'
65+
@options
66+
end
67+
end
68+
69+
assert_raise_message(
70+
'Error connecting to Redis on 127.0.0.5:999 (Errno::ECONNREFUSED)'
71+
) do
72+
new_redis = _new_client(connector: custom_connector)
73+
new_redis.ping
74+
end
75+
end
5976
end

0 commit comments

Comments
 (0)