Skip to content

Commit

Permalink
Merge pull request #591 from tgxworld/allow_custom_connectors
Browse files Browse the repository at this point in the history
Add option to use a different Connector.
  • Loading branch information
byroot authored Jun 4, 2018
2 parents bc80cb1 + 088b8c6 commit ddf058b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
1 change: 1 addition & 0 deletions lib/redis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def self.current=(redis)
# @option options [Boolean] :inherit_socket (false) Whether to use socket in forked process or not
# @option options [Array] :sentinels List of sentinels to contact
# @option options [Symbol] :role (:master) Role to fetch via Sentinel, either `:master` or `:slave`
# @option options [Class] :connector Class of custom connector
#
# @return [Redis] a new client instance
def initialize(options = {})
Expand Down
13 changes: 8 additions & 5 deletions lib/redis/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,14 @@ def initialize(options = {})

@pending_reads = 0

if options.include?(:sentinels)
@connector = Connector::Sentinel.new(@options)
else
@connector = Connector.new(@options)
end
@connector =
if options.include?(:sentinels)
Connector::Sentinel.new(@options)
elsif options.include?(:connector) && options[:connector].respond_to?(:new)
options.delete(:connector).new(@options)
else
Connector.new(@options)
end
end

def connect
Expand Down
17 changes: 17 additions & 0 deletions test/client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,21 @@ def test_queue_after_error

assert_equal result, ["OK", 1]
end

def test_client_with_custom_connector
custom_connector = Class.new(Redis::Client::Connector) do
def resolve
@options[:host] = '127.0.0.5'
@options[:port] = '999'
@options
end
end

assert_raise_message(
'Error connecting to Redis on 127.0.0.5:999 (Errno::ECONNREFUSED)'
) do
new_redis = _new_client(connector: custom_connector)
new_redis.ping
end
end
end

0 comments on commit ddf058b

Please sign in to comment.