Skip to content

Commit 625caf0

Browse files
committed
Fix Request with DNS issue not retried
1 parent 22cb1a7 commit 625caf0

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

RetryableHttpClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public function request(string $method, string $url, array $options = []): Respo
127127

128128
$context->getResponse()->cancel();
129129

130-
$delay = $this->getDelayFromHeader($context->getHeaders()) ?? $this->strategy->getDelay($context, $chunk->isLast() ? $content : null, $exception);
130+
$delay = $this->getDelayFromHeader($context->getHeaders()) ?? $this->strategy->getDelay($context, !$exception && $chunk->isLast() ? $content : null, $exception);
131131
++$retryCount;
132132

133133
$this->logger->info('Try #{count} after {delay}ms'.($exception ? ': '.$exception->getMessage() : ', status code: '.$context->getStatusCode()), [

Tests/RetryableHttpClientTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PHPUnit\Framework\TestCase;
66
use Symfony\Component\HttpClient\Exception\ServerException;
77
use Symfony\Component\HttpClient\MockHttpClient;
8+
use Symfony\Component\HttpClient\NativeHttpClient;
89
use Symfony\Component\HttpClient\Response\AsyncContext;
910
use Symfony\Component\HttpClient\Response\MockResponse;
1011
use Symfony\Component\HttpClient\Retry\GenericRetryStrategy;
@@ -133,4 +134,29 @@ public function testStreamNoRetry()
133134
}
134135
}
135136
}
137+
138+
public function testRetryWithDnsIssue()
139+
{
140+
$client = new RetryableHttpClient(
141+
new NativeHttpClient(),
142+
new class(GenericRetryStrategy::DEFAULT_RETRY_STATUS_CODES, 0) extends GenericRetryStrategy {
143+
public function shouldRetry(AsyncContext $context, ?string $responseContent, ?TransportExceptionInterface $exception): ?bool
144+
{
145+
$this->fail('should not be called');
146+
}
147+
},
148+
2,
149+
$logger = new TestLogger()
150+
);
151+
152+
$response = $client->request('GET', 'http://does.not.exists/foo-bar');
153+
154+
try {
155+
$response->getHeaders();
156+
} catch (TransportExceptionInterface $e) {
157+
$this->assertSame('Could not resolve host "does.not.exists".', $e->getMessage());
158+
}
159+
$this->assertCount(2, $logger->logs);
160+
$this->assertSame('Try #{count} after {delay}ms: Could not resolve host "does.not.exists".', $logger->logs[0]);
161+
}
136162
}

0 commit comments

Comments
 (0)