Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Curl connect timeout #841

Merged
merged 2 commits into from
May 15, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means setting the timeout for all clients that don't implement it to 5 seconds. Isn't the default (without setting it) 300? So it would make sense to keep the behaviour the same as before to set it to 300? Or what is the default currently used?


/**
* 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