Skip to content

Commit

Permalink
[HttpClient] Fix computing retry delay when using RetryableHttpClient
Browse files Browse the repository at this point in the history
  • Loading branch information
martkop26 authored and nicolas-grekas committed Sep 8, 2022
1 parent 1c6db72 commit 5fee724
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion RetryableHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ private function getDelayFromHeader(array $headers): ?int
{
if (null !== $after = $headers['retry-after'][0] ?? null) {
if (is_numeric($after)) {
return (int) $after * 1000;
return (int) ($after * 1000);
}

if (false !== $time = strtotime($after)) {
Expand Down
38 changes: 38 additions & 0 deletions Tests/RetryableHttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,42 @@ public function testCancelOnTimeout()
$response->cancel();
}
}

public function testRetryWithDelay()
{
$retryAfter = '0.46';

$client = new RetryableHttpClient(
new MockHttpClient([
new MockResponse('', [
'http_code' => 503,
'response_headers' => [
'retry-after' => $retryAfter,
],
]),
new MockResponse('', [
'http_code' => 200,
]),
]),
new GenericRetryStrategy(),
1,
$logger = new class() extends TestLogger {
public array $context = [];

public function log($level, $message, array $context = []): void
{
$this->context = $context;
parent::log($level, $message, $context);
}
},
);

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

$delay = $logger->context['delay'] ?? null;

$this->assertArrayHasKey('delay', $logger->context);
$this->assertNotNull($delay);
$this->assertSame((int) ($retryAfter * 1000), $delay);
}
}

0 comments on commit 5fee724

Please sign in to comment.