Skip to content

Commit

Permalink
CURLOPT_CONNECTTIMEOUT added to the HTTP transport curl options. Supp…
Browse files Browse the repository at this point in the history
…ort for a custom connection timeout through a connectTimeout parameter.
  • Loading branch information
rmruano committed May 14, 2015
1 parent dfe06cc commit c29490f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
4 changes: 4 additions & 0 deletions changes.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
CHANGES

2015-05-14
- CURLOPT_CONNECTTIMEOUT added to the HTTP transport curl options
- Support for a custom connection timeout through a connectTimeout parameter

2015-05-11
- Release 2.0.0
- Update elasticsearch dependency to elasticsearch 1.5.2 https://www.elastic.co/downloads/past-releases/elasticsearch-1-5-2 #834
Expand Down
23 changes: 23 additions & 0 deletions lib/Elastica/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ class Connection extends Param
*/
const TIMEOUT = 300;

/**
* Number of seconds after a connection timeout occurs for every request
* Use a small value if you need a fast response in case of dead servers.
*/
const CONNECT_TIMEOUT = 5;

/**
* Creates a new connection object. A connection is enabled by default
*
Expand Down Expand Up @@ -159,6 +165,23 @@ public function getTimeout()
return (int) $this->hasParam('timeout') ? $this->getParam('timeout') : self::TIMEOUT;
}

/**
* @param int $timeout Connect timeout in seconds
* @return $this
*/
public function setConnectTimeout($timeout)
{
return $this->setParam('connectTimeout', $timeout);
}

/**
* @return int Connection timeout in seconds
*/
public function getConnectTimeout()
{
return (int) $this->hasParam('connectTimeout') ? $this->getParam('connectTimeout') : self::CONNECT_TIMEOUT;
}

/**
* Enables a connection
*
Expand Down
1 change: 1 addition & 0 deletions lib/Elastica/Transport/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public function exec(Request $request, array $params)

curl_setopt($conn, CURLOPT_URL, $baseUri);
curl_setopt($conn, CURLOPT_TIMEOUT, $connection->getTimeout());
curl_setopt($conn, CURLOPT_CONNECTTIMEOUT, $connection->getConnectTimeout());
curl_setopt($conn, CURLOPT_FORBID_REUSE, 0);

$proxy = $connection->getProxy();
Expand Down
1 change: 1 addition & 0 deletions test/lib/Elastica/Test/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function testEmptyConstructor()
$this->assertEquals(Connection::DEFAULT_TRANSPORT, $connection->getTransport());
$this->assertInstanceOf('Elastica\Transport\AbstractTransport', $connection->getTransportObject());
$this->assertEquals(Connection::TIMEOUT, $connection->getTimeout());
$this->assertEquals(Connection::CONNECT_TIMEOUT, $connection->getConnectTimeout());
$this->assertEquals(array(), $connection->getConfig());
$this->assertTrue($connection->isEnabled());
}
Expand Down

0 comments on commit c29490f

Please sign in to comment.