| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\HttpClient; |
| 13 |
|
| 14 |
use Psr\Log\LoggerInterface; |
| 15 |
use Symfony\Component\HttpClient\Response\AsyncContext; |
| 16 |
use Symfony\Component\HttpClient\Response\AsyncResponse; |
| 17 |
use Symfony\Component\HttpClient\Retry\GenericRetryStrategy; |
| 18 |
use Symfony\Component\HttpClient\Retry\RetryStrategyInterface; |
| 19 |
use Symfony\Contracts\HttpClient\ChunkInterface; |
| 20 |
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; |
| 21 |
use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 22 |
use Symfony\Contracts\HttpClient\ResponseInterface; |
| 23 |
use Symfony\Contracts\Service\ResetInterface; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
om> |
| 29 |
|
| 30 |
class RetryableHttpClient implements HttpClientInterface, ResetInterface |
| 31 |
{ |
| 32 |
use AsyncDecoratorTrait; |
| 33 |
|
| 34 |
private RetryStrategyInterface $strategy; |
| 35 |
private array $baseUris = []; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public function __construct( |
| 41 |
HttpClientInterface $client, |
| 42 |
?RetryStrategyInterface $strategy = null, |
| 43 |
private int $maxRetries = 3, |
| 44 |
private ?LoggerInterface $logger = null, |
| 45 |
) { |
| 46 |
$this->client = $client; |
| 47 |
$this->strategy = $strategy ?? new GenericRetryStrategy(); |
| 48 |
} |
| 49 |
|
| 50 |
public function withOptions(array $options): static |
| 51 |
{ |
| 52 |
if (\array_key_exists('base_uri', $options)) { |
| 53 |
if (\is_array($options['base_uri'])) { |
| 54 |
$this->baseUris = $options['base_uri']; |
| 55 |
unset($options['base_uri']); |
| 56 |
} else { |
| 57 |
$this->baseUris = []; |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
$clone = clone $this; |
| 62 |
$clone->maxRetries = (int) ($options['max_retries'] ?? $this->maxRetries); |
| 63 |
unset($options['max_retries']); |
| 64 |
|
| 65 |
$clone->client = $this->client->withOptions($options); |
| 66 |
|
| 67 |
return $clone; |
| 68 |
} |
| 69 |
|
| 70 |
public function request(string $method, string $url, array $options = []): ResponseInterface |
| 71 |
{ |
| 72 |
$baseUris = \array_key_exists('base_uri', $options) ? $options['base_uri'] : $this->baseUris; |
| 73 |
$baseUris = \is_array($baseUris) ? $baseUris : []; |
| 74 |
$options = self::shiftBaseUri($options, $baseUris); |
| 75 |
|
| 76 |
$maxRetries = (int) ($options['max_retries'] ?? $this->maxRetries); |
| 77 |
unset($options['max_retries']); |
| 78 |
|
| 79 |
if ($maxRetries <= 0) { |
| 80 |
return new AsyncResponse($this->client, $method, $url, $options); |
| 81 |
} |
| 82 |
|
| 83 |
return new AsyncResponse($this->client, $method, $url, $options, function (ChunkInterface $chunk, AsyncContext $context) use ($method, $url, $options, $maxRetries, &$baseUris) { |
| 84 |
static $retryCount = 0; |
| 85 |
static $content = ''; |
| 86 |
static $firstChunk; |
| 87 |
|
| 88 |
$exception = null; |
| 89 |
try { |
| 90 |
if ($context->getInfo('canceled') || $chunk->isTimeout() || null !== $chunk->getInformationalStatus()) { |
| 91 |
yield $chunk; |
| 92 |
|
| 93 |
return; |
| 94 |
} |
| 95 |
} catch (TransportExceptionInterface $exception) { |
| 96 |
|
| 97 |
} |
| 98 |
if (null !== $exception) { |
| 99 |
|
| 100 |
if ('' !== $context->getInfo('primary_ip')) { |
| 101 |
$shouldRetry = $this->strategy->shouldRetry($context, null, $exception); |
| 102 |
if (null === $shouldRetry) { |
| 103 |
throw new \LogicException(\sprintf('The "%s::shouldRetry()" method must not return null when called with an exception.', $this->strategy::class)); |
| 104 |
} |
| 105 |
|
| 106 |
if (false === $shouldRetry) { |
| 107 |
yield from $this->passthru($context, $firstChunk, $content, $chunk); |
| 108 |
|
| 109 |
return; |
| 110 |
} |
| 111 |
} |
| 112 |
} elseif ($chunk->isFirst()) { |
| 113 |
if (false === $shouldRetry = $this->strategy->shouldRetry($context, null, null)) { |
| 114 |
yield from $this->passthru($context, $firstChunk, $content, $chunk); |
| 115 |
|
| 116 |
return; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
if (null === $shouldRetry) { |
| 121 |
$firstChunk = $chunk; |
| 122 |
$content = ''; |
| 123 |
|
| 124 |
return; |
| 125 |
} |
| 126 |
} else { |
| 127 |
if (!$chunk->isLast()) { |
| 128 |
$content .= $chunk->getContent(); |
| 129 |
|
| 130 |
return; |
| 131 |
} |
| 132 |
|
| 133 |
if (null === $shouldRetry = $this->strategy->shouldRetry($context, $content, null)) { |
| 134 |
throw new \LogicException(\sprintf('The "%s::shouldRetry()" method must not return null when called with a body.', $this->strategy::class)); |
| 135 |
} |
| 136 |
|
| 137 |
if (false === $shouldRetry) { |
| 138 |
yield from $this->passthru($context, $firstChunk, $content, $chunk); |
| 139 |
|
| 140 |
return; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
$context->getResponse()->cancel(); |
| 145 |
|
| 146 |
$delay = $this->getDelayFromHeader($context->getHeaders()) ?? $this->strategy->getDelay($context, !$exception && $chunk->isLast() ? $content : null, $exception); |
| 147 |
++$retryCount; |
| 148 |
$content = ''; |
| 149 |
$firstChunk = null; |
| 150 |
|
| 151 |
$this->logger?->info('Try #{count} after {delay}ms'.($exception ? ': '.$exception->getMessage() : ', status code: '.$context->getStatusCode()), [ |
| 152 |
'count' => $retryCount, |
| 153 |
'delay' => $delay, |
| 154 |
]); |
| 155 |
|
| 156 |
$context->setInfo('retry_count', $retryCount); |
| 157 |
$context->replaceRequest($method, $url, self::shiftBaseUri($options, $baseUris)); |
| 158 |
$context->pause($delay / 1000); |
| 159 |
|
| 160 |
if ($retryCount >= $maxRetries) { |
| 161 |
$context->passthru(); |
| 162 |
} |
| 163 |
}); |
| 164 |
} |
| 165 |
|
| 166 |
private function getDelayFromHeader(array $headers): ?int |
| 167 |
{ |
| 168 |
if (null !== $after = $headers['retry-after'][0] ?? null) { |
| 169 |
if (is_numeric($after)) { |
| 170 |
return (int) ($after * 1000); |
| 171 |
} |
| 172 |
|
| 173 |
if (false !== $time = strtotime($after)) { |
| 174 |
return max(0, $time - time()) * 1000; |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
return null; |
| 179 |
} |
| 180 |
|
| 181 |
private function passthru(AsyncContext $context, ?ChunkInterface $firstChunk, string &$content, ChunkInterface $lastChunk): \Generator |
| 182 |
{ |
| 183 |
$context->passthru(); |
| 184 |
|
| 185 |
if (null !== $firstChunk) { |
| 186 |
yield $firstChunk; |
| 187 |
} |
| 188 |
|
| 189 |
if ('' !== $content) { |
| 190 |
$chunk = $context->createChunk($content); |
| 191 |
$content = ''; |
| 192 |
|
| 193 |
yield $chunk; |
| 194 |
} |
| 195 |
|
| 196 |
yield $lastChunk; |
| 197 |
} |
| 198 |
|
| 199 |
private static function shiftBaseUri(array $options, array &$baseUris): array |
| 200 |
{ |
| 201 |
if ($baseUris) { |
| 202 |
$baseUri = 1 < \count($baseUris) ? array_shift($baseUris) : current($baseUris); |
| 203 |
$options['base_uri'] = \is_array($baseUri) ? $baseUri[array_rand($baseUri)] : $baseUri; |
| 204 |
} elseif (\is_array($options['base_uri'] ?? null)) { |
| 205 |
unset($options['base_uri']); |
| 206 |
} |
| 207 |
|
| 208 |
return $options; |
| 209 |
} |
| 210 |
} |