From 885d37d9aab0f0c390d86f2709ade6a9a54bad90 Mon Sep 17 00:00:00 2001 From: Romain Neutron Date: Thu, 12 Nov 2020 15:51:19 +0100 Subject: [PATCH] Use microsecond precision instead of second for connection timeout and connect-timeout --- CHANGELOG.md | 1 + src/Transport/Http.php | 20 ++++++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 160a4e933c..0aedde6264 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/src/Transport/Http.php b/src/Transport/Http.php index d1ddce7c93..98eb4ca2fb 100644 --- a/src/Transport/Http.php +++ b/src/Transport/Http.php @@ -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 @@ -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()) {