Skip to content

Commit

Permalink
Add retry backoff.
Browse files Browse the repository at this point in the history
  • Loading branch information
kporras07 committed Jan 3, 2024
1 parent bd06590 commit b7c4eb1
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Request/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ private function createRetryDecider(): callable
{
$config = $this->getConfig();
$maxRetries = $config->get('http_max_retries', 5);
$retryBackoff = $config->get('http_retry_backoff', 5);
$logger = $this->logger;
$logWarning = function (string $message) use ($logger) {
if ($this->output()->isVerbose()) {
Expand All @@ -191,7 +192,8 @@ private function createRetryDecider(): callable
?Exception $exception = null
) use (
$maxRetries,
$logWarning
$logWarning,
$retryBackoff
) {
$logWarningOnRetry = fn (string $reason) => 0 === $retry
? $logWarning(
Expand All @@ -217,6 +219,8 @@ private function createRetryDecider(): callable
// Retry on connection-related exceptions such as "Connection refused" and "Operation timed out".
if ($retry !== $maxRetries) {
$logWarningOnRetry($exception->getMessage());
$logWarning(sprintf("Retrying in %s seconds.", $retryBackoff * ($retry + 1)));
sleep($retryBackoff * ($retry + 1));

return true;
}
Expand All @@ -240,6 +244,9 @@ private function createRetryDecider(): callable
['body' => $response->getBody()->getContents()]
);

$logWarning(sprintf("Retrying in %s seconds.", $retryBackoff * ($retry + 1)));
sleep($retryBackoff * ($retry + 1));

return true;
}
}
Expand Down

0 comments on commit b7c4eb1

Please sign in to comment.