Skip to content

Commit c29490f

Browse files
committed
CURLOPT_CONNECTTIMEOUT added to the HTTP transport curl options. Support for a custom connection timeout through a connectTimeout parameter.
1 parent dfe06cc commit c29490f

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

changes.txt

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
CHANGES
22

3+
2015-05-14
4+
- CURLOPT_CONNECTTIMEOUT added to the HTTP transport curl options
5+
- Support for a custom connection timeout through a connectTimeout parameter
6+
37
2015-05-11
48
- Release 2.0.0
59
- Update elasticsearch dependency to elasticsearch 1.5.2 https://www.elastic.co/downloads/past-releases/elasticsearch-1-5-2 #834

lib/Elastica/Connection.php

+23
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ class Connection extends Param
3737
*/
3838
const TIMEOUT = 300;
3939

40+
/**
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.
43+
*/
44+
const CONNECT_TIMEOUT = 5;
45+
4046
/**
4147
* Creates a new connection object. A connection is enabled by default
4248
*
@@ -159,6 +165,23 @@ public function getTimeout()
159165
return (int) $this->hasParam('timeout') ? $this->getParam('timeout') : self::TIMEOUT;
160166
}
161167

168+
/**
169+
* @param int $timeout Connect timeout in seconds
170+
* @return $this
171+
*/
172+
public function setConnectTimeout($timeout)
173+
{
174+
return $this->setParam('connectTimeout', $timeout);
175+
}
176+
177+
/**
178+
* @return int Connection timeout in seconds
179+
*/
180+
public function getConnectTimeout()
181+
{
182+
return (int) $this->hasParam('connectTimeout') ? $this->getParam('connectTimeout') : self::CONNECT_TIMEOUT;
183+
}
184+
162185
/**
163186
* Enables a connection
164187
*

lib/Elastica/Transport/Http.php

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ 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());
7374
curl_setopt($conn, CURLOPT_FORBID_REUSE, 0);
7475

7576
$proxy = $connection->getProxy();

test/lib/Elastica/Test/ConnectionTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public function testEmptyConstructor()
1717
$this->assertEquals(Connection::DEFAULT_TRANSPORT, $connection->getTransport());
1818
$this->assertInstanceOf('Elastica\Transport\AbstractTransport', $connection->getTransportObject());
1919
$this->assertEquals(Connection::TIMEOUT, $connection->getTimeout());
20+
$this->assertEquals(Connection::CONNECT_TIMEOUT, $connection->getConnectTimeout());
2021
$this->assertEquals(array(), $connection->getConfig());
2122
$this->assertTrue($connection->isEnabled());
2223
}

0 commit comments

Comments
 (0)