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

@ -49,6 +49,11 @@ enable_maintenance_mode = false
[http]
; Operation timeout in seconds
timeout = 30
; Operation retry count in case of curl error
retries = 2
; User agent
useragent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Firefox/102.0"
; Max http response size in MB

View file

@ -38,6 +38,7 @@ function getContents(
$config = [
'useragent' => Configuration::getConfig('http', 'useragent'),
'timeout' => Configuration::getConfig('http', 'timeout'),
'retries' => Configuration::getConfig('http', 'retries'),
'headers' => array_merge($defaultHttpHeaders, $httpHeadersNormalized),
'curl_options' => $curlOptions,
];
@ -71,7 +72,7 @@ function getContents(
// Ignore invalid 'Last-Modified' HTTP header value
}
}
// todo: to be nice nice citizen we should also check for Etag
// todo: We should also check for Etag
}
$response = $httpClient->request($url, $config);

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);