feat(http-client): add http retry count to config (#3887)

This commit is contained in:
Dag 2024-01-10 21:48:12 +01:00 committed by GitHub
parent c7e8ddf486
commit 080e29365a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 15 deletions

View file

@ -63,7 +63,7 @@ final class CurlHttpClient implements HttpClient
'proxy' => null,
'curl_options' => [],
'if_not_modified_since' => null,
'retries' => 3,
'retries' => 2,
'max_filesize' => null,
'max_redirections' => 5,
];
@ -136,26 +136,28 @@ final class CurlHttpClient implements HttpClient
return $len;
});
$attempts = 0;
// This retry logic is a bit hard to understand, but it works
$tries = 0;
while (true) {
$attempts++;
$tries++;
$body = curl_exec($ch);
if ($body !== false) {
// The network call was successful, so break out of the loop
break;
}
if ($attempts > $config['retries']) {
// Finally give up
$curl_error = curl_error($ch);
$curl_errno = curl_errno($ch);
throw new HttpException(sprintf(
'cURL error %s: %s (%s) for %s',
$curl_error,
$curl_errno,
'https://curl.haxx.se/libcurl/c/libcurl-errors.html',
$url
));
if ($tries <= $config['retries']) {
continue;
}
// Max retries reached, give up
$curl_error = curl_error($ch);
$curl_errno = curl_errno($ch);
throw new HttpException(sprintf(
'cURL error %s: %s (%s) for %s',
$curl_error,
$curl_errno,
'https://curl.haxx.se/libcurl/c/libcurl-errors.html',
$url
));
}
$statusCode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);