File tree Expand file tree Collapse file tree 5 files changed +16
-6
lines changed Expand file tree Collapse file tree 5 files changed +16
-6
lines changed Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ class Client
12
12
:port => 6379 ,
13
13
:path => nil ,
14
14
:timeout => 5.0 ,
15
+ :connect_timeout => 5.0 ,
15
16
:password => nil ,
16
17
:db => 0 ,
17
18
:driver => nil ,
@@ -408,6 +409,12 @@ def _parse_options(options)
408
409
end
409
410
410
411
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
+
411
418
options [ :db ] = options [ :db ] . to_i
412
419
options [ :driver ] = _parse_driver ( options [ :driver ] ) || Connection . drivers . last
413
420
Original file line number Diff line number Diff line change @@ -11,9 +11,9 @@ def self.connect(config)
11
11
connection = ::Hiredis ::Connection . new
12
12
13
13
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 ) )
15
15
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 ) )
17
17
end
18
18
19
19
instance = new ( connection )
Original file line number Diff line number Diff line change @@ -206,9 +206,9 @@ class Ruby
206
206
207
207
def self . connect ( config )
208
208
if config [ :scheme ] == "unix"
209
- sock = UNIXSocket . connect ( config [ :path ] , config [ :timeout ] )
209
+ sock = UNIXSocket . connect ( config [ :path ] , config [ :connect_timeout ] )
210
210
else
211
- sock = TCPSocket . connect ( config [ :host ] , config [ :port ] , config [ :timeout ] )
211
+ sock = TCPSocket . connect ( config [ :host ] , config [ :port ] , config [ :connect_timeout ] )
212
212
end
213
213
214
214
instance = new ( sock )
Original file line number Diff line number Diff line change @@ -70,7 +70,7 @@ def self.connect(config)
70
70
conn = EventMachine . connect_unix_domain ( config [ :path ] , RedisClient )
71
71
else
72
72
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
74
74
end
75
75
end
76
76
Original file line number Diff line number Diff line change @@ -152,9 +152,12 @@ def test_time
152
152
end
153
153
154
154
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
155
157
assert_raise Redis ::CannotConnectError do
156
- Redis . new ( OPTIONS . merge ( :host => "10.255.255.254" , :timeout => 0.1 ) ) . ping
158
+ Redis . new ( opts ) . ping
157
159
end
160
+ assert ( Time . now - start_time ) <= opts [ :timeout ]
158
161
end
159
162
160
163
def close_on_ping ( seq , options = { } )
You can’t perform that action at this time.
0 commit comments