Skip to content

Commit

Permalink
Use microsecond precision instead of second for connection timeout an…
Browse files Browse the repository at this point in the history
…d connect-timeout
  • Loading branch information
romainneutron committed Nov 12, 2020
1 parent ebde220 commit 885d37d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Used new mapping endpoints classes [#1845](https://github.com/ruflin/Elastica/pull/1845)
* Used new nodes endpoints classes [#1863](https://github.com/ruflin/Elastica/pull/1863)
* Used new settings endpoints classes [#1852](https://github.com/ruflin/Elastica/pull/1852)
* Allow float values for connection timeout and connection connect-timeout, providing ms precision for those. Previous precision was second. [#xxx](https://github.com/ruflin/Elastica/pull/xxx)
### Deprecated
* Deprecated `Elastica\Aggregation\Range::setKeyedResponse()`, use `setKeyed()` instead [#1848](https://github.com/ruflin/Elastica/pull/1848)
* Deprecated `Elastica\Exception\ResponseException::getElasticsearchException()`, use `getResponse()::getFullError()` instead [#1829](https://github.com/ruflin/Elastica/pull/1829)
Expand Down
20 changes: 18 additions & 2 deletions src/Transport/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,14 @@ public function exec(Request $request, array $params): Response
}

\curl_setopt($conn, \CURLOPT_URL, $baseUri);
\curl_setopt($conn, \CURLOPT_TIMEOUT, $connection->getTimeout());

// If cURL >= 7.16.2, timeout can be expressed in ms
if (\defined('CURLOPT_TIMEOUT_MS')) {
\curl_setopt($conn, \CURLOPT_TIMEOUT_MS, \round($connection->getTimeout() * 1000));
} else {
\curl_setopt($conn, \CURLOPT_TIMEOUT, \round($connection->getTimeout()));
}

\curl_setopt($conn, \CURLOPT_FORBID_REUSE, 0);

// Tell ES that we support the compressed responses
Expand All @@ -87,7 +94,16 @@ public function exec(Request $request, array $params): Response
/* @see Connection::setConnectTimeout() */
$connectTimeout = $connection->getConnectTimeout();
if ($connectTimeout > 0) {
\curl_setopt($conn, \CURLOPT_CONNECTTIMEOUT, $connectTimeout);
// If cURL >= 7.16.2, timeout can be expressed in ms
if (\defined('CURLOPT_CONNECTTIMEOUT_MS')) {
// since we're accepting float, and the option uses an int, let's round and use a
// minimum value of "1" for the option. "0" means "no timeout"
\curl_setopt($conn, \CURLOPT_CONNECTTIMEOUT_MS, \max(1, \round($connectTimeout * 1000)));
} else {
// since we're accepting float, and the option uses an int, let's round and use a
// minimum value of "1" for the option. "0" means "no timeout"
\curl_setopt($conn, \CURLOPT_CONNECTTIMEOUT, \max(1, \round($connectTimeout)));
}
}

if (null !== $proxy = $connection->getProxy()) {
Expand Down

0 comments on commit 885d37d

Please sign in to comment.