Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[HttpClient] cURL: Unable to limit connection timeout #48513

Closed
@deeky666

Description

@deeky666

Symfony version(s) affected

4.4.x and higher

Description

In #33022 the CURL_CONNECTTIMEOUT_MS was removed but it was not added to the allow list of curl.extra again. As such its not possible to set this setting, causing the timeout / max_duration option to be used when connecting to a host that is not available (port not open).

See #47246 (comment)

When not being able to distinguish between connection timeout and max duration, it is not possible to set a reasonable timeout in order to let requests fail fast on connection issues and still allow requests to take quite some time to finish overall. Especially in combination with a retry strategy this is a big problem if you do not want to let users wait for a long time when a host is unavailable.

How to reproduce

<?php

use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\HttpClient\Exception\TimeoutException;

require_once __DIR__ . '/vendor/autoload.php';

$client = HttpClient::create();
var_dump(get_class($client));
$ts = hrtime(true);
try {
    $response = $client->request(
        'GET',
        'http://172.67.72.38:1080',
        options: ['timeout' => 3, 'max_duration' => 10]
    );

    $messages = $response->toArray();
} catch (TimeoutException $e) {
    var_dump($e->getMessage());
}
echo "Duration: " . ((hrtime(true) - $ts) / 1e+6) . "\n";

/*
string(43) "Symfony\Component\HttpClient\CurlHttpClient"
string(53) "Idle timeout reached for "http://172.67.72.38:1080/"."
Duration: 3003.314591
*/

$h = curl_init('http://172.67.72.38:1080');
curl_setopt_array($h, [
    CURLOPT_CONNECTTIMEOUT => 2,
    CURLOPT_TIMEOUT => 10,
    CURLOPT_VERBOSE => true,
]);

curl_exec($h);

/*
*   Trying 172.67.72.38:1080...
* After 2000ms connect time, move on!
* connect to 172.67.72.38 port 1080 failed: Connection timed out
* Connection timeout after 2001 ms
* Closing connection 0
 * 
 */

Possible Solution

Allow setting CURLOPT_CONNECTTIMEOUT and CURLOPT_CONNECTTIMEOUT_MS via curl.extra.

Additional Context

#33022
#32807
#47246

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions