8000 [HttpClient] Make retry strategy work again by Nyholm · Pull Request #53889 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[HttpClient] Make retry strategy work again #53889

New issue

Have a question about this proje 8000 ct? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Symfony/Component/HttpClient/Response/AsyncResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ public function __construct(HttpClientInterface $client, string $method, string
while (true) {
foreach (self::stream([$response], $timeout) as $chunk) {
if ($chunk->isTimeout() && $response->passthru) {
foreach (self::passthru($response->client, $response, new ErrorChunk($response->offset, $chunk->getError())) as $chunk) {
// Timeouts thrown during initialization are transport errors
foreach (self::passthru($response->client, $response, new ErrorChunk($response->offset, new TransportException($chunk->getError()))) as $chunk) {
if ($chunk->isFirst()) {
return false;
}
Expand Down
41 changes: 23 additions & 18 deletions src/Symfony/Component/HttpClient/Tests/RetryableHttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpClient\Exception\ServerException;
use Symfony\Component\HttpClient\Exception\TimeoutException;
use Symfony\Component\HttpClient\Exception\TransportException;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\NativeHttpClient;
use Symfony\Component\HttpClient\Response\AsyncContext;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Component\HttpClient\Retry\GenericRetryStrategy;
use Symfony\Component\HttpClient\Retry\RetryStrategyInterface;
use Symfony\Component\HttpClient\RetryableHttpClient;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\Test\TestHttpServer;
Expand Down Expand Up @@ -247,32 +248,36 @@ public function testRetryOnErrorAssertContent()
self::assertSame('Test out content', $response->getContent(), 'Content should be buffered');
}

/**
* @testWith ["GET"]
* ["POST"]
* ["PUT"]
* ["PATCH"]
* ["DELETE"]
*/
public function testRetryOnHeaderTimeout(string $method)
public function testRetryOnTimeout()
{
$client = HttpClient::create();

if ($client instanceof NativeHttpClient) {
$this->markTestSkipped('NativeHttpClient cannot timeout before receiving headers');
}

TestHttpServer::start();

$client = new RetryableHttpClient($client);
$response = $client->request($method, 'http://localhost:8057/timeout-header', ['timeout' => 0.1]);
$strategy = new class() implements RetryStrategyInterface {
public $isCalled = false;

public function shouldRetry(AsyncContext $context, ?string $responseContent, ?TransportExceptionInterface $exception): ?bool
{
$this->isCalled = true;

return false;
}

public function getDelay(AsyncContext $context, ?string $responseContent, ?TransportExceptionInterface $exception): int
{
return 0;
}
};
$client = new RetryableHttpClient($client, $strategy);
$response = $client->request('GET', 'http://localhost:8057/timeout-header', ['timeout' => 0.1]);

try {
$response->getStatusCode();
$this->fail(TimeoutException::class.' expected');
} catch (TimeoutException $e) {
$this->fail(TransportException::class.' expected');
} catch (TransportException $e) {
}

$this->assertSame('Idle timeout reached for "http://localhost:8057/timeout-header".', $response->getInfo('error'));
$this->assertTrue($strategy->isCalled, 'The HTTP retry strategy should be called');
}
}
0