Fix Request with DNS issue not retried

This commit is contained in:
Jérémy Derussé 2021-02-03 14:01:01 +01:00
parent 90ace3e11f
commit 216abd0307
No known key found for this signature in database
GPG Key ID: 2083FA5758C473D2
2 changed files with 27 additions and 1 deletions

View File

@ -127,7 +127,7 @@ class RetryableHttpClient implements HttpClientInterface
$context->getResponse()->cancel();
$delay = $this->getDelayFromHeader($context->getHeaders()) ?? $this->strategy->getDelay($context, $chunk->isLast() ? $content : null, $exception);
$delay = $this->getDelayFromHeader($context->getHeaders()) ?? $this->strategy->getDelay($context, !$exception && $chunk->isLast() ? $content : null, $exception);
++$retryCount;
$this->logger->info('Try #{count} after {delay}ms'.($exception ? ': '.$exception->getMessage() : ', status code: '.$context->getStatusCode()), [

View File

@ -5,6 +5,7 @@ namespace Symfony\Component\HttpClient\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpClient\Exception\ServerException;
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;
@ -133,4 +134,29 @@ class RetryableHttpClientTest extends TestCase
}
}
}
public function testRetryWithDnsIssue()
{
$client = new RetryableHttpClient(
new NativeHttpClient(),
new class(GenericRetryStrategy::DEFAULT_RETRY_STATUS_CODES, 0) extends GenericRetryStrategy {
public function shouldRetry(AsyncContext $context, ?string $responseContent, ?TransportExceptionInterface $exception): ?bool
{
$this->fail('should not be called');
}
},
2,
$logger = new TestLogger()
);
$response = $client->request('GET', 'http://does.not.exists/foo-bar');
try {
$response->getHeaders();
} catch (TransportExceptionInterface $e) {
$this->assertSame('Could not resolve host "does.not.exists".', $e->getMessage());
}
$this->assertCount(2, $logger->logs);
$this->assertSame('Try #{count} after {delay}ms: Could not resolve host "does.not.exists".', $logger->logs[0]);
}
}