Skip to content

Commit 878ffd2

Browse files
committed
Merge pull request #464 from csfrancis/add_connect_timeout
Add :connect_timeout option
2 parents 916a418 + a37e5ae commit 878ffd2

File tree

5 files changed

+16
-6
lines changed

5 files changed

+16
-6
lines changed

lib/redis/client.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class Client
1212
:port => 6379,
1313
:path => nil,
1414
:timeout => 5.0,
15+
:connect_timeout => 5.0,
1516
:password => nil,
1617
:db => 0,
1718
:driver => nil,
@@ -408,6 +409,12 @@ def _parse_options(options)
408409
end
409410

410411
options[:timeout] = options[:timeout].to_f
412+
options[:connect_timeout] = if options[:connect_timeout]
413+
options[:connect_timeout].to_f
414+
else
415+
options[:timeout]
416+
end
417+
411418
options[:db] = options[:db].to_i
412419
options[:driver] = _parse_driver(options[:driver]) || Connection.drivers.last
413420

lib/redis/connection/hiredis.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ def self.connect(config)
1111
connection = ::Hiredis::Connection.new
1212

1313
if config[:scheme] == "unix"
14-
connection.connect_unix(config[:path], Integer(config[:timeout] * 1_000_000))
14+
connection.connect_unix(config[:path], Integer(config[:connect_timeout] * 1_000_000))
1515
else
16-
connection.connect(config[:host], config[:port], Integer(config[:timeout] * 1_000_000))
16+
connection.connect(config[:host], config[:port], Integer(config[:connect_timeout] * 1_000_000))
1717
end
1818

1919
instance = new(connection)

lib/redis/connection/ruby.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,9 @@ class Ruby
206206

207207
def self.connect(config)
208208
if config[:scheme] == "unix"
209-
sock = UNIXSocket.connect(config[:path], config[:timeout])
209+
sock = UNIXSocket.connect(config[:path], config[:connect_timeout])
210210
else
211-
sock = TCPSocket.connect(config[:host], config[:port], config[:timeout])
211+
sock = TCPSocket.connect(config[:host], config[:port], config[:connect_timeout])
212212
end
213213

214214
instance = new(sock)

lib/redis/connection/synchrony.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def self.connect(config)
7070
conn = EventMachine.connect_unix_domain(config[:path], RedisClient)
7171
else
7272
conn = EventMachine.connect(config[:host], config[:port], RedisClient) do |c|
73-
c.pending_connect_timeout = [config[:timeout], 0.1].max
73+
c.pending_connect_timeout = [config[:connect_timeout], 0.1].max
7474
end
7575
end
7676

test/internals_test.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,12 @@ def test_time
152152
end
153153

154154
def test_connection_timeout
155+
opts = OPTIONS.merge(:host => "10.255.255.254", :connect_timeout => 0.1, :timeout => 5.0)
156+
start_time = Time.now
155157
assert_raise Redis::CannotConnectError do
156-
Redis.new(OPTIONS.merge(:host => "10.255.255.254", :timeout => 0.1)).ping
158+
Redis.new(opts).ping
157159
end
160+
assert (Time.now - start_time) <= opts[:timeout]
158161
end
159162

160163
def close_on_ping(seq, options = {})

0 commit comments

Comments
 (0)