Skip to content

Commit b9ccf37

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

File tree

7 files changed

+21
-21
lines changed

7 files changed

+21
-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: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313

1414
use Psr\Log\LoggerInterface;
1515
use Psr\Log\NullLogger;
16+
use Symfony\Component\HttpClient\Chunk\ErrorChunk;
1617
use Symfony\Component\HttpClient\Response\AsyncContext;
1718
use Symfony\Component\HttpClient\Response\AsyncResponse;
19+
use Symfony\Component\HttpClient\Response\MockResponse;
1820
use Symfony\Component\HttpClient\Retry\ExponentialBackOff;
1921
use Symfony\Component\HttpClient\Retry\HttpCodeDecider;
2022
use Symfony\Component\HttpClient\Retry\RetryBackOffInterface;
@@ -57,16 +59,14 @@ public function request(string $method, string $url, array $options = []): Respo
5759
return new AsyncResponse($this->client, $method, $url, $options, function (ChunkInterface $chunk, AsyncContext $context) use ($method, $url, $options, &$retryCount) {
5860
$exception = null;
5961
try {
60-
// clone the chunk to let the original value of `$didthrow`
61-
$clonedChunk = clone $chunk;
62-
if ($clonedChunk->isTimeout() || null !== $clonedChunk->getInformationalStatus()) {
62+
if ($chunk->isTimeout() || null !== $chunk->getInformationalStatus()) {
6363
yield $chunk;
6464

6565
return;
6666
}
6767

6868
// only retry first chunk
69-
if (!$clonedChunk->isFirst()) {
69+
if (!$chunk->isFirst()) {
7070
$context->passthru();
7171
yield $chunk;
7272

@@ -76,32 +76,37 @@ public function request(string $method, string $url, array $options = []): Respo
7676
// catch TransportExceptionInterface to send it to strategy.
7777
}
7878

79-
if ($retryCount >= $this->maxRetries || !$this->decider->shouldRetry($method, $url, $options, $response = $context->getResponse(), $exception)) {
79+
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)) {
8080
$context->passthru();
8181
yield $chunk;
8282

8383
return;
8484
}
8585

8686
$context->setInfo('retry_count', $retryCount);
87-
$response->cancel();
87+
$context->getResponse()->cancel();
8888

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

92-
$this->logger->info('Error returned by the server. Retrying #{retryCount} using {delay} ms delay: '.($exception ? $exception->getMessage() : 'StatusCode: '.$response->getStatusCode()), [
92+
$this->logger->info('Error returned by the server. Retrying #{retryCount} using {delay} ms delay: '.($exception ? $exception->getMessage() : 'StatusCode: '.$statusCode), [
9393
'retryCount' => $retryCount,
9494
'delay' => $delay,
9595
]);
9696

97+
try {
98+
$chunk->getContent();
99+
} catch (TransportExceptionInterface $exception) {
100+
// silent the failsafe exception
101+
}
97102
$context->replaceRequest($method, $url, $options);
98103
$context->pause($delay / 1000);
99104
});
100105
}
101106

102-
private function getDelayFromHeader(ResponseInterface $response): ?int
107+
private function getDelayFromHeader(array $headers): ?int
103108
{
104-
if (null !== $after = $response->getHeaders(false)['retry-after'][0] ?? null) {
109+
if (null !== $after = $headers['retry-after'][0] ?? null) {
105110
if (is_numeric($after)) {
106111
return (int) $after * 1000;
107112
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy