mirror of
https://github.com/RSS-Bridge/rss-bridge.git
synced 2025-03-14 20:21:14 +03:00
feat(http-client): add http retry count to config (#3887)
This commit is contained in:
parent
c7e8ddf486
commit
080e29365a
3 changed files with 23 additions and 15 deletions
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
|
|
30
lib/http.php
30
lib/http.php
|
@ -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);
|
||||
|
|
Loading…
Add table
Reference in a new issue