GenericRetryStrategy.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. /**
  35. * @var mixed[]
  36. */
  37. private $statusCodes;
  38. /**
  39. * @var int
  40. */
  41. private $delayMs;
  42. /**
  43. * @var float
  44. */
  45. private $multiplier;
  46. /**
  47. * @var int
  48. */
  49. private $maxDelayMs;
  50. /**
  51. * @var float
  52. */
  53. private $jitter;
  54. /**
  55. * @param array $statusCodes List of HTTP status codes that trigger a retry
  56. * @param int $delayMs Amount of time to delay (or the initial value when multiplier is used)
  57. * @param float $multiplier Multiplier to apply to the delay each time a retry occurs
  58. * @param int $maxDelayMs Maximum delay to allow (0 means no maximum)
  59. * @param float $jitter Probability of randomness int delay (0 = none, 1 = 100% random)
  60. */
  61. public function __construct($statusCodes = self::DEFAULT_RETRY_STATUS_CODES, $delayMs = 1000, $multiplier = 2.0, $maxDelayMs = 0, $jitter = 0.1)
  62. {
  63. $this->statusCodes = $statusCodes;
  64. if ($delayMs < 0) {
  65. throw new InvalidArgumentException(sprintf('Delay must be greater than or equal to zero: "%s" given.', $delayMs));
  66. }
  67. $this->delayMs = $delayMs;
  68. if ($multiplier < 1) {
  69. throw new InvalidArgumentException(sprintf('Multiplier must be greater than or equal to one: "%s" given.', $multiplier));
  70. }
  71. $this->multiplier = $multiplier;
  72. if ($maxDelayMs < 0) {
  73. throw new InvalidArgumentException(sprintf('Max delay must be greater than or equal to zero: "%s" given.', $maxDelayMs));
  74. }
  75. $this->maxDelayMs = $maxDelayMs;
  76. if ($jitter < 0 || $jitter > 1) {
  77. throw new InvalidArgumentException(sprintf('Jitter must be between 0 and 1: "%s" given.', $jitter));
  78. }
  79. $this->jitter = $jitter;
  80. }
  81. /**
  82. * @param \Symfony\Component\HttpClient\Response\AsyncContext $context
  83. * @param string|null $responseContent
  84. * @param \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface|null $exception
  85. */
  86. public function shouldRetry($context, $responseContent, $exception)
  87. {
  88. $statusCode = $context->getStatusCode();
  89. if (\in_array($statusCode, $this->statusCodes, true)) {
  90. return true;
  91. }
  92. if (isset($this->statusCodes[$statusCode]) && \is_array($this->statusCodes[$statusCode])) {
  93. return \in_array($context->getInfo('http_method'), $this->statusCodes[$statusCode], true);
  94. }
  95. if (null === $exception) {
  96. return false;
  97. }
  98. if (\in_array(0, $this->statusCodes, true)) {
  99. return true;
  100. }
  101. if (isset($this->statusCodes[0]) && \is_array($this->statusCodes[0])) {
  102. return \in_array($context->getInfo('http_method'), $this->statusCodes[0], true);
  103. }
  104. return false;
  105. }
  106. /**
  107. * @param \Symfony\Component\HttpClient\Response\AsyncContext $context
  108. * @param string|null $responseContent
  109. * @param \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface|null $exception
  110. */
  111. public function getDelay($context, $responseContent, $exception)
  112. {
  113. $delay = $this->delayMs * $this->multiplier ** $context->getInfo('retry_count');
  114. if ($this->jitter > 0) {
  115. $randomness = (int) ($delay * $this->jitter);
  116. $delay = $delay + random_int(-$randomness, +$randomness);
  117. }
  118. if ($delay > $this->maxDelayMs && 0 !== $this->maxDelayMs) {
  119. return $this->maxDelayMs;
  120. }
  121. return (int) $delay;
  122. }
  123. }