Skip to content

Commit

Permalink
Merge branch '6.0' into 6.1
Browse files Browse the repository at this point in the history
* 6.0:
  Typos In Comments
  Typos on 6.0 branch
  remove duplicated catch block
  s/<\br>/<br>
  [HttpClient] Fix retrying requests when the content is used by the strategy
  • Loading branch information
fabpot committed Oct 28, 2022
2 parents fa931a5 + af84893 commit f515d06
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
4 changes: 3 additions & 1 deletion RetryableHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function request(string $method, string $url, array $options = []): Respo
return new AsyncResponse($this->client, $method, $url, $options, function (ChunkInterface $chunk, AsyncContext $context) use ($method, $url, $options, &$retryCount, &$content, &$firstChunk) {
$exception = null;
try {
if ($chunk->isTimeout() || null !== $chunk->getInformationalStatus() || $context->getInfo('canceled')) {
if ($context->getInfo('canceled') || $chunk->isTimeout() || null !== $chunk->getInformationalStatus()) {
yield $chunk;

return;
Expand Down Expand Up @@ -118,6 +118,8 @@ public function request(string $method, string $url, array $options = []): Respo

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

$this->logger->info('Try #{count} after {delay}ms'.($exception ? ': '.$exception->getMessage() : ', status code: '.$context->getStatusCode()), [
'count' => $retryCount,
Expand Down
9 changes: 5 additions & 4 deletions Tests/RetryableHttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,22 @@ public function testRetryWithBody()
{
$client = new RetryableHttpClient(
new MockHttpClient([
new MockResponse('', ['http_code' => 500]),
new MockResponse('', ['http_code' => 200]),
new MockResponse('abc', ['http_code' => 500]),
new MockResponse('def', ['http_code' => 200]),
]),
new class(GenericRetryStrategy::DEFAULT_RETRY_STATUS_CODES, 0) extends GenericRetryStrategy {
public function shouldRetry(AsyncContext $context, ?string $responseContent, ?TransportExceptionInterface $exception): ?bool
{
return null === $responseContent ? null : 200 !== $context->getStatusCode();
return 500 === $context->getStatusCode() && null === $responseContent ? null : 200 !== $context->getStatusCode();
}
},
1
2
);

$response = $client->request('GET', 'http://example.com/foo-bar');

self::assertSame(200, $response->getStatusCode());
self::assertSame('def', $response->getContent());
}

public function testRetryWithBodyKeepContent()
Expand Down

0 comments on commit f515d06

Please sign in to comment.