Skip to content

Commit

Permalink
feat(http-client): add http retry count to config
Browse files Browse the repository at this point in the history
  • Loading branch information
dvikan committed Jan 10, 2024
1 parent 491cb50 commit fbda03a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
5 changes: 5 additions & 0 deletions config.default.ini.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@
[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
Expand Down
3 changes: 2 additions & 1 deletion lib/contents.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
Expand Down Expand Up @@ -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);
Expand Down
30 changes: 16 additions & 14 deletions lib/http.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function request(string $url, array $config = []): Response
'proxy' => null,
'curl_options' => [],
'if_not_modified_since' => null,
'retries' => 3,
'retries' => 2,
'max_filesize' => null,
'max_redirections' => 5,
];
Expand Down Expand Up @@ -136,26 +136,28 @@ public function request(string $url, array $config = []): Response
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);
Expand Down

0 comments on commit fbda03a

Please sign in to comment.