Skip to content

Commit 3a538b1

Browse files
committed
Don't set CURLOPT_CONNECTTIMEOUT if connectTimeout is 0. CONNECT_TIMEOUT constant set to 0.
1 parent c29490f commit 3a538b1

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

changes.txt

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
CHANGES
22

33
2015-05-14
4-
- CURLOPT_CONNECTTIMEOUT added to the HTTP transport curl options
5-
- Support for a custom connection timeout through a connectTimeout parameter
4+
- Support for a custom connection timeout through a connectTimeout parameter. CURLOPT_CONNECTTIMEOUT added to the HTTP transport curl options if a connectTimeout is >0 #841
65

76
2015-05-11
87
- Release 2.0.0

lib/Elastica/Connection.php

+10-4
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ class Connection extends Param
3838
const TIMEOUT = 300;
3939

4040
/**
41-
* Number of seconds after a connection timeout occurs for every request
42-
* Use a small value if you need a fast response in case of dead servers.
41+
* Number of seconds after a connection timeout occurs for every request during the connection phase.
42+
* @see Connection::setConnectTimeout();
4343
*/
44-
const CONNECT_TIMEOUT = 5;
44+
const CONNECT_TIMEOUT = 0;
4545

4646
/**
4747
* Creates a new connection object. A connection is enabled by default
@@ -166,6 +166,12 @@ public function getTimeout()
166166
}
167167

168168
/**
169+
* Number of seconds after a connection timeout occurs for every request during the connection phase.
170+
* Use a small value if you need a fast fail in case of dead, unresponsive or unreachable servers (~5 sec).
171+
*
172+
* Set to zero to switch to the default built-in connection timeout (300 seconds in curl).
173+
* @see http://curl.haxx.se/libcurl/c/CURLOPT_CONNECTTIMEOUT.html
174+
*
169175
* @param int $timeout Connect timeout in seconds
170176
* @return $this
171177
*/
@@ -179,7 +185,7 @@ public function setConnectTimeout($timeout)
179185
*/
180186
public function getConnectTimeout()
181187
{
182-
return (int) $this->hasParam('connectTimeout') ? $this->getParam('connectTimeout') : self::CONNECT_TIMEOUT;
188+
return (int) $this->hasParam('connectTimeout') ? $this->getParam('connectTimeout') : self::CONNECT_TIMEOUT;
183189
}
184190

185191
/**

lib/Elastica/Transport/Http.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,14 @@ public function exec(Request $request, array $params)
7070

7171
curl_setopt($conn, CURLOPT_URL, $baseUri);
7272
curl_setopt($conn, CURLOPT_TIMEOUT, $connection->getTimeout());
73-
curl_setopt($conn, CURLOPT_CONNECTTIMEOUT, $connection->getConnectTimeout());
7473
curl_setopt($conn, CURLOPT_FORBID_REUSE, 0);
7574

75+
/* @see Connection::setConnectTimeout() */
76+
$connectTimeout = $connection->getConnectTimeout();
77+
if ($connectTimeout>0) {
78+
curl_setopt($conn, CURLOPT_CONNECTTIMEOUT, $connection->getConnectTimeout());
79+
}
80+
7681
$proxy = $connection->getProxy();
7782

7883
// See: https://github.com/facebook/hhvm/issues/4875

0 commit comments

Comments
 (0)