GenericRetryStrategy.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpClient\Retry;
  11. use Symfony\Component\HttpClient\Exception\InvalidArgumentException;
  12. use Symfony\Component\HttpClient\Response\AsyncContext;
  13. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  14. /**
  15. * Decides to retry the request when HTTP status codes belong to the given list of codes.
  16. *
  17. * @author Jérémy Derussé <jeremy@derusse.com>
  18. */
  19. class GenericRetryStrategy implements RetryStrategyInterface
  20. {
  21. public const IDEMPOTENT_METHODS = ['GET', 'HEAD', 'PUT', 'DELETE', 'OPTIONS', 'TRACE'];
  22. public const DEFAULT_RETRY_STATUS_CODES = [
  23. 0 => self::IDEMPOTENT_METHODS, // for transport exceptions
  24. 423,
  25. 425,
  26. 429,
  27. 500 => self::IDEMPOTENT_METHODS,
  28. 502,
  29. 503,
  30. 504 => self::IDEMPOTENT_METHODS,
  31. 507 => self::IDEMPOTENT_METHODS,
  32. 510 => self::IDEMPOTENT_METHODS,
  33. ];
  34. private $statusCodes;
  35. private $delayMs;
  36. private $multiplier;
  37. private $maxDelayMs;
  38. private $jitter;
  39. /**
  40. * @param array $statusCodes List of HTTP status codes that trigger a retry
  41. * @param int $delayMs Amount of time to delay (or the initial value when multiplier is used)
  42. * @param float $multiplier Multiplier to apply to the delay each time a retry occurs
  43. * @param int $maxDelayMs Maximum delay to allow (0 means no maximum)
  44. * @param float $jitter Probability of randomness int delay (0 = none, 1 = 100% random)
  45. */
  46. public function __construct(array $statusCodes = self::DEFAULT_RETRY_STATUS_CODES, int $delayMs = 1000, float $multiplier = 2.0, int $maxDelayMs = 0, float $jitter = 0.1)
  47. {
  48. $this->statusCodes = $statusCodes;
  49. if ($delayMs < 0) {
  50. throw new InvalidArgumentException(sprintf('Delay must be greater than or equal to zero: "%s" given.', $delayMs));
  51. }
  52. $this->delayMs = $delayMs;
  53. if ($multiplier < 1) {
  54. throw new InvalidArgumentException(sprintf('Multiplier must be greater than or equal to one: "%s" given.', $multiplier));
  55. }
  56. $this->multiplier = $multiplier;
  57. if ($maxDelayMs < 0) {
  58. throw new InvalidArgumentException(sprintf('Max delay must be greater than or equal to zero: "%s" given.', $maxDelayMs));
  59. }
  60. $this->maxDelayMs = $maxDelayMs;
  61. if ($jitter < 0 || $jitter > 1) {
  62. throw new InvalidArgumentException(sprintf('Jitter must be between 0 and 1: "%s" given.', $jitter));
  63. }
  64. $this->jitter = $jitter;
  65. }
  66. public function shouldRetry(AsyncContext $context, ?string $responseContent, ?TransportExceptionInterface $exception): ?bool
  67. {
  68. $statusCode = $context->getStatusCode();
  69. if (\in_array($statusCode, $this->statusCodes, true)) {
  70. return true;
  71. }
  72. if (isset($this->statusCodes[$statusCode]) && \is_array($this->statusCodes[$statusCode])) {
  73. return \in_array($context->getInfo('http_method'), $this->statusCodes[$statusCode], true);
  74. }
  75. if (null === $exception) {
  76. return false;
  77. }
  78. if (\in_array(0, $this->statusCodes, true)) {
  79. return true;
  80. }
  81. if (isset($this->statusCodes[0]) && \is_array($this->statusCodes[0])) {
  82. return \in_array($context->getInfo('http_method'), $this->statusCodes[0], true);
  83. }
  84. return false;
  85. }
  86. public function getDelay(AsyncContext $context, ?string $responseContent, ?TransportExceptionInterface $exception): int
  87. {
  88. $delay = $this->delayMs * $this->multiplier ** $context->getInfo('retry_count');
  89. if ($this->jitter > 0) {
  90. $randomness = $delay * $this->jitter;
  91. $delay = $delay + random_int(-$randomness, +$randomness);
  92. }
  93. if ($delay > $this->maxDelayMs && 0 !== $this->maxDelayMs) {
  94. return $this->maxDelayMs;
  95. }
  96. return (int) $delay;
  97. }
  98. }