Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
5 changes: 5 additions & 0 deletions src/Symfony/Component/HttpClient/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.4
---

* Add `max_retries` option to `RetryableHttpClient` to adjust the retry logic on a per request level

6.3
---

Expand Down
12 changes: 9 additions & 3 deletions src/Symfony/Component/HttpClient/RetryableHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public function withOptions(array $options): static
}

$clone = clone $this;
$clone->maxRetries = (int) ($options['max_retries'] ?? $this->maxRetries);
unset($options['max_retries']);

$clone->client = $this->client->withOptions($options);

return $clone;
Expand All @@ -71,11 +74,14 @@ public function request(string $method, string $url, array $options = []): Respo
$baseUris = \is_array($baseUris) ? $baseUris : [];
$options = self::shiftBaseUri($options, $baseUris);

if ($this->maxRetries <= 0) {
$maxRetries = (int) ($options['max_retries'] ?? $this->maxRetries);
unset($options['max_retries']);

if ($maxRetries <= 0) {
return new AsyncResponse($this->client, $method, $url, $options);
}

return new AsyncResponse($this->client, $method, $url, $options, function (ChunkInterface $chunk, AsyncContext $context) use ($method, $url, $options, &$baseUris) {
return new AsyncResponse($this->client, $method, $url, $options, function (ChunkInterface $chunk, AsyncContext $context) use ($method, $url, $options, $maxRetries, &$baseUris) {
static $retryCount = 0;
static $content = '';
static $firstChunk;
Expand Down Expand Up @@ -152,7 +158,7 @@ public function request(string $method, string $url, array $options = []): Respo
$context->replaceRequest($method, $url, self::shiftBaseUri($options, $baseUris));
$context->pause($delay / 1000);

if ($retryCount >= $this->maxRetries) {
if ($retryCount >= $maxRetries) {
$context->passthru();
}
});
Expand Down
41 changes: 41 additions & 0 deletions src/Symfony/Component/HttpClient/Tests/RetryableHttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,4 +344,45 @@ public function testRetryWithMultipleBaseUrisPreservesNonNestedOrder()
self::assertSame(200, $response->getStatusCode());
self::assertSame('http://example.com/d/foo-bar', $response->getInfo('url'));
}

public function testMaxRetriesOption()
{
$client = new RetryableHttpClient(
new MockHttpClient([
new MockResponse('', ['http_code' => 500]),
new MockResponse('', ['http_code' => 502]),
new MockResponse('', ['http_code' => 200]),
]),
new GenericRetryStrategy([500, 502], 0),
3
);

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

self::assertSame(502, $response->getStatusCode());
}

public function testMaxRetriesWithOptions()
{
$client = new RetryableHttpClient(
new MockHttpClient([
new MockResponse('', ['http_code' => 500]),
new MockResponse('', ['http_code' => 502]),
new MockResponse('', ['http_code' => 504]),
new MockResponse('', ['http_code' => 200]),
]),
new GenericRetryStrategy([500, 502, 504], 0),
3
);

$client = $client->withOptions([
'max_retries' => 2,
]);

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

self::assertSame(504, $response->getStatusCode());
}
}