Skip to content

Commit 6523df7

Browse files
committed
Do not send ResponseInterface to decider/backoff
1 parent ecfdf60 commit 6523df7

File tree

7 files changed

+15
-21
lines changed

7 files changed

+15
-21
lines changed

src/Symfony/Bundle/FrameworkBundle/Tests/Command/CacheClearCommand/Fixture/.preload.php

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1486,7 +1486,7 @@ public function testHttpClientOverrideDefaultOptions()
14861486

14871487
public function testHttpClientRetry()
14881488
{
1489-
if (!\class_exists(RetryHttpClient::class)) {
1489+
if (!class_exists(RetryHttpClient::class)) {
14901490
$this->expectException(LogicException::class);
14911491
}
14921492
$container = $this->createContainerFromFile('http_client_retry');

src/Symfony/Component/HttpClient/Retry/ExponentialBackOff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function __construct(int $delayMilliseconds = 1000, float $multiplier = 2
5757
$this->maxDelayMilliseconds = $maxDelayMilliseconds;
5858
}
5959

60-
public function getDelay(int $retryCount, string $requestMethod, string $requestUrl, array $requestOptions, ResponseInterface $response, \Throwable $throwable = null): int
60+
public function getDelay(int $retryCount, string $requestMethod, string $requestUrl, array $requestOptions, ResponseInterface $partialResponse, \Throwable $throwable = null): int
6161
{
6262
$delay = $this->delayMilliseconds * $this->multiplier ** $retryCount;
6363

src/Symfony/Component/HttpClient/Retry/HttpCodeDecider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ public function __construct(array $statusCodes = [423, 425, 429, 500, 502, 503,
3131
$this->statusCodes = $statusCodes;
3232
}
3333

34-
public function shouldRetry(string $requestMethod, string $requestUrl, array $requestOptions, ResponseInterface $response, \Throwable $throwable = null): bool
34+
public function shouldRetry(string $requestMethod, string $requestUrl, array $requestOptions, ResponseInterface $partialResponse, \Throwable $throwable = null): bool
3535
{
3636
if ($throwable instanceof TransportExceptionInterface) {
3737
return true;
3838
}
3939

40-
return \in_array($response->getStatusCode(), $this->statusCodes, true);
40+
return \in_array($partialResponse->getStatusCode(), $this->statusCodes, true);
4141
}
4242
}

src/Symfony/Component/HttpClient/Retry/RetryBackOffInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ interface RetryBackOffInterface
2121
/**
2222
* Returns the time to wait in milliseconds.
2323
*/
24-
public function getDelay(int $retryCount, string $requestMethod, string $requestUrl, array $requestOptions, ResponseInterface $response, \Throwable $throwable = null): int;
24+
public function getDelay(int $retryCount, string $requestMethod, string $requestUrl, array $requestOptions, ResponseInterface $partialResponse, \Throwable $throwable = null): int;
2525
}

src/Symfony/Component/HttpClient/Retry/RetryDeciderInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ interface RetryDeciderInterface
2121
/**
2222
* Returns whether the request should be retried.
2323
*/
24-
public function shouldRetry(string $requestMethod, string $requestUrl, array $requestOptions, ResponseInterface $response, \Throwable $throwable = null): bool;
24+
public function shouldRetry(string $requestMethod, string $requestUrl, array $requestOptions, ResponseInterface $partialResponse, \Throwable $throwable = null): bool;
2525
}

src/Symfony/Component/HttpClient/RetryHttpClient.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Psr\Log\NullLogger;
1616
use Symfony\Component\HttpClient\Response\AsyncContext;
1717
use Symfony\Component\HttpClient\Response\AsyncResponse;
18+
use Symfony\Component\HttpClient\Response\MockResponse;
1819
use Symfony\Component\HttpClient\Retry\ExponentialBackOff;
1920
use Symfony\Component\HttpClient\Retry\HttpCodeDecider;
2021
use Symfony\Component\HttpClient\Retry\RetryBackOffInterface;
@@ -57,16 +58,14 @@ public function request(string $method, string $url, array $options = []): Respo
5758
return new AsyncResponse($this->client, $method, $url, $options, function (ChunkInterface $chunk, AsyncContext $context) use ($method, $url, $options, &$retryCount) {
5859
$exception = null;
5960
try {
60-
// clone the chunk to let the original value of `$didthrow`
61-
$clonedChunk = clone $chunk;
62-
if ($clonedChunk->isTimeout() || null !== $clonedChunk->getInformationalStatus()) {
61+
if ($chunk->isTimeout() || null !== $chunk->getInformationalStatus()) {
6362
yield $chunk;
6463

6564
return;
6665
}
6766

6867
// only retry first chunk
69-
if (!$clonedChunk->isFirst()) {
68+
if (!$chunk->isFirst()) {
7069
$context->passthru();
7170
yield $chunk;
7271

@@ -76,20 +75,20 @@ public function request(string $method, string $url, array $options = []): Respo
7675
// catch TransportExceptionInterface to send it to strategy.
7776
}
7877

79-
if ($retryCount >= $this->maxRetries || !$this->decider->shouldRetry($method, $url, $options, $response = $context->getResponse(), $exception)) {
78+
if ($retryCount >= $this->maxRetries || !$this->decider->shouldRetry($method, $url, $options, $partialResponse = new MockResponse($context->getContent(), ['http_code' => $statusCode = $context->getStatusCode(), 'headers' => $headers = $context->getHeaders()]), $exception)) {
8079
$context->passthru();
8180
yield $chunk;
8281

8382
return;
8483
}
8584

8685
$context->setInfo('retry_count', $retryCount);
87-
$response->cancel();
86+
$context->getResponse()->cancel();
8887

89-
$delay = $this->getDelayFromHeader($response) ?? $this->strategy->getDelay($retryCount, $method, $url, $options, $response = $context->getResponse(), $exception);
88+
$delay = $this->getDelayFromHeader($headers) ?? $this->strategy->getDelay($retryCount, $method, $url, $options, $partialResponse, $exception);
9089
++$retryCount;
9190

92-
$this->logger->info('Error returned by the server. Retrying #{retryCount} using {delay} ms delay: '.($exception ? $exception->getMessage() : 'StatusCode: '.$response->getStatusCode()), [
91+
$this->logger->info('Error returned by the server. Retrying #{retryCount} using {delay} ms delay: '.($exception ? $exception->getMessage() : 'StatusCode: '.$statusCode), [
9392
'retryCount' => $retryCount,
9493
'delay' => $delay,
9594
]);
@@ -99,9 +98,9 @@ public function request(string $method, string $url, array $options = []): Respo
9998
});
10099
}
101100

102-
private function getDelayFromHeader(ResponseInterface $response): ?int
101+
private function getDelayFromHeader(array $headers): ?int
103102
{
104-
if (null !== $after = $response->getHeaders(false)['retry-after'][0] ?? null) {
103+
if (null !== $after = $headers['retry-after'][0] ?? null) {
105104
if (is_numeric($after)) {
106105
return (int) $after * 1000;
107106
}

0 commit comments

Comments
 (0)