From 81a636ad39ffb589a8c4531517972c0cf3797302 Mon Sep 17 00:00:00 2001 From: siliconforks Date: Mon, 9 Jun 2025 05:18:47 -0300 Subject: [PATCH] Allow sub-second timeouts where feasible This implements the suggestion made in #264 to check for CURL_VERSION_ASYNCHDNS to determine whether timeouts of less than 1 second will work. See also: https://core.trac.wordpress.org/ticket/63547 --- src/Transport/Curl.php | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/Transport/Curl.php b/src/Transport/Curl.php index 18de09d2a..521d42774 100644 --- a/src/Transport/Curl.php +++ b/src/Transport/Curl.php @@ -439,11 +439,24 @@ private function setup_handle($url, $headers, $data, $options) { // cURL requires a minimum timeout of 1 second when using the system // DNS resolver, as it uses `alarm()`, which is second resolution only. - // There's no way to detect which DNS resolver is being used from our - // end, so we need to round up regardless of the supplied timeout. // // https://github.com/curl/curl/blob/4f45240bc84a9aa648c8f7243be7b79e9f9323a5/lib/hostip.c#L606-L609 - $timeout = max($options['timeout'], 1); + $using_asynch_dns = false; + if (defined('CURL_VERSION_ASYNCHDNS')) { + $curl_version = curl_version(); + // phpcs:ignore PHPCompatibility.Constants.NewConstants.curl_version_asynchdnsFound + if (CURL_VERSION_ASYNCHDNS & $curl_version['features']) { + $using_asynch_dns = true; + } + } + + if ($using_asynch_dns) { + // It should be safe to use any timeout, even if less than 1 second. + $timeout = $options['timeout']; + } else { + // If the timeout is less than 1 second, we need to round up. + $timeout = max($options['timeout'], 1); + } if (is_int($timeout) || $this->version < self::CURL_7_16_2) { curl_setopt($this->handle, CURLOPT_TIMEOUT, ceil($timeout));