-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[HttpClient] simplify retry mechanism around RetryStrategyInterface #38532
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[HttpClient] simplify retry mechanism around RetryStrategyInterface
- Loading branch information
commit 544c3e867825dca2468673386245a0db2c065b2c
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 0 additions & 81 deletions
81
src/Symfony/Component/HttpClient/Retry/ExponentialBackOff.php
This file was deleted.
Oops, something went wrong.
84 changes: 84 additions & 0 deletions
84
src/Symfony/Component/HttpClient/Retry/GenericRetryStrategy.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpClient\Retry; | ||
|
||
use Symfony\Component\HttpClient\Response\AsyncContext; | ||
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; | ||
|
||
/** | ||
* Decides to retry the request when HTTP status codes belong to the given list of codes. | ||
* | ||
* @author Jérémy Derussé <jeremy@derusse.com> | ||
*/ | ||
class GenericRetryStrategy implements RetryStrategyInterface | ||
{ | ||
public const DEFAULT_RETRY_STATUS_CODES = [423, 425, 429, 500, 502, 503, 504, 507, 510]; | ||
|
||
private $statusCodes; | ||
private $delayMs; | ||
private $multiplier; | ||
private $maxDelayMs; | ||
private $jitter; | ||
|
||
/** | ||
* @param array $statusCodes List of HTTP status codes that trigger a retry | ||
* @param int $delayMs Amount of time to delay (or the initial value when multiplier is used) | ||
* @param float $multiplier Multiplier to apply to the delay each time a retry occurs | ||
* @param int $maxDelayMs Maximum delay to allow (0 means no maximum) | ||
* @param float $jitter Probability of randomness int delay (0 = none, 1 = 100% random) | ||
*/ | ||
public function __construct(array $statusCodes = self::DEFAULT_RETRY_STATUS_CODES, int $delayMs = 1000, float $multiplier = 2.0, int $maxDelayMs = 0, float $jitter = 0.1) | ||
{ | ||
$this->statusCodes = $statusCodes; | ||
|
||
if ($delayMs < 0) { | ||
throw new InvalidArgumentException(sprintf('Delay must be greater than or equal to zero: "%s" given.', $delayMs)); | ||
} | ||
$this->delayMs = $delayMs; | ||
|
||
if ($multiplier < 1) { | ||
throw new InvalidArgumentException(sprintf('Multiplier must be greater than or equal to one: "%s" given.', $multiplier)); | ||
} | ||
$this->multiplier = $multiplier; | ||
|
||
if ($maxDelayMs < 0) { | ||
throw new InvalidArgumentException(sprintf('Max delay must be greater than or equal to zero: "%s" given.', $maxDelayMs)); | ||
} | ||
$this->maxDelayMs = $maxDelayMs; | ||
|
||
if ($jitter < 0 || $jitter > 1) { | ||
throw new InvalidArgumentException(sprintf('Jitter must be between 0 and 1: "%s" given.', $jitter)); | ||
} | ||
$this->jitter = $jitter; | ||
} | ||
|
||
public function shouldRetry(AsyncContext $context, ?string $responseContent, ?TransportExceptionInterface $exception): ?bool | ||
{ | ||
return \in_array($context->getStatusCode(), $this->statusCodes, true); | ||
} | ||
|
||
public function getDelay(AsyncContext $context, ?string $responseContent, ?TransportExceptionInterface $exception): int | ||
{ | ||
$delay = $this->delayMs * $this->multiplier ** $context->getInfo('retry_count'); | ||
|
||
if ($this->jitter > 0) { | ||
$randomness = $delay * $this->jitter; | ||
$delay = $delay + random_int(-$randomness, +$randomness); | ||
} | ||
|
||
if ($delay > $this->maxDelayMs && 0 !== $this->maxDelayMs) { | ||
return $this->maxDelayMs; | ||
} | ||
|
||
return (int) $delay; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should document that its delay implementation is an exponential backoff. That got lost when naming it
generic
.