| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\HttpClient\Retry; |
| 13 |
|
| 14 |
use Symfony\Component\HttpClient\Exception\InvalidArgumentException; |
| 15 |
use Symfony\Component\HttpClient\Response\AsyncContext; |
| 16 |
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
om> |
| 22 |
|
| 23 |
class GenericRetryStrategy implements RetryStrategyInterface |
| 24 |
{ |
| 25 |
public const IDEMPOTENT_METHODS = ['GET', 'HEAD', 'PUT', 'DELETE', 'OPTIONS', 'TRACE', 'QUERY']; |
| 26 |
public const DEFAULT_RETRY_STATUS_CODES = [ |
| 27 |
0 => self::IDEMPOTENT_METHODS, |
| 28 |
423, |
| 29 |
425, |
| 30 |
429, |
| 31 |
500 => self::IDEMPOTENT_METHODS, |
| 32 |
502, |
| 33 |
503, |
| 34 |
504 => self::IDEMPOTENT_METHODS, |
| 35 |
507 => self::IDEMPOTENT_METHODS, |
| 36 |
510 => self::IDEMPOTENT_METHODS, |
| 37 |
]; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public function __construct( |
| 47 |
private array $statusCodes = self::DEFAULT_RETRY_STATUS_CODES, |
| 48 |
private int $delayMs = 1000, |
| 49 |
private float $multiplier = 2.0, |
| 50 |
private int $maxDelayMs = 0, |
| 51 |
private float $jitter = 0.1, |
| 52 |
) { |
| 53 |
if ($delayMs < 0) { |
| 54 |
throw new InvalidArgumentException(\sprintf('Delay must be greater than or equal to zero: "%s" given.', $delayMs)); |
| 55 |
} |
| 56 |
|
| 57 |
if ($multiplier < 1) { |
| 58 |
throw new InvalidArgumentException(\sprintf('Multiplier must be greater than or equal to one: "%s" given.', $multiplier)); |
| 59 |
} |
| 60 |
|
| 61 |
if ($maxDelayMs < 0) { |
| 62 |
throw new InvalidArgumentException(\sprintf('Max delay must be greater than or equal to zero: "%s" given.', $maxDelayMs)); |
| 63 |
} |
| 64 |
|
| 65 |
if ($jitter < 0 || $jitter > 1) { |
| 66 |
throw new InvalidArgumentException(\sprintf('Jitter must be between 0 and 1: "%s" given.', $jitter)); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
public function shouldRetry(AsyncContext $context, ?string $responseContent, ?TransportExceptionInterface $exception): ?bool |
| 71 |
{ |
| 72 |
$statusCode = $context->getStatusCode(); |
| 73 |
if (\in_array($statusCode, $this->statusCodes, true)) { |
| 74 |
return true; |
| 75 |
} |
| 76 |
if (isset($this->statusCodes[$statusCode]) && \is_array($this->statusCodes[$statusCode])) { |
| 77 |
return \in_array($context->getInfo('http_method'), $this->statusCodes[$statusCode], true); |
| 78 |
} |
| 79 |
if (null === $exception) { |
| 80 |
return false; |
| 81 |
} |
| 82 |
|
| 83 |
if (\in_array(0, $this->statusCodes, true)) { |
| 84 |
return true; |
| 85 |
} |
| 86 |
if (isset($this->statusCodes[0]) && \is_array($this->statusCodes[0])) { |
| 87 |
return \in_array($context->getInfo('http_method'), $this->statusCodes[0], true); |
| 88 |
} |
| 89 |
|
| 90 |
return false; |
| 91 |
} |
| 92 |
|
| 93 |
public function getDelay(AsyncContext $context, ?string $responseContent, ?TransportExceptionInterface $exception): int |
| 94 |
{ |
| 95 |
$delay = $this->delayMs * $this->multiplier ** $context->getInfo('retry_count'); |
| 96 |
|
| 97 |
if ($this->jitter > 0) { |
| 98 |
$randomness = (int) ($delay * $this->jitter); |
| 99 |
$delay += random_int(-$randomness, +$randomness); |
| 100 |
} |
| 101 |
|
| 102 |
if ($delay > $this->maxDelayMs && 0 !== $this->maxDelayMs) { |
| 103 |
return $this->maxDelayMs; |
| 104 |
} |
| 105 |
|
| 106 |
return (int) $delay; |
| 107 |
} |
| 108 |
} |