8000 [HttpClient] Fix decorating progress info in AsyncResponse by jderusse · Pull Request #38633 · symfony/symfony · GitHub
[go: up one dir, main page]

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
Original file line number Diff line number Diff line change
Expand Up @@ -2097,7 +2097,7 @@ private function registerRetryableHttpClient(array $options, string $name, Conta

$container
->register($name.'.retryable', RetryableHttpClient::class)
->setDecoratedService($name, null, -10) // lower priority than TraceableHttpClient
->setDecoratedService($name, null, 10) // higher priority than TraceableHttpClient
->setArguments([new Reference($name.'.retryable.inner'), $retryStrategy, $options['max_retries'], new Reference('logger')])
->addTag('monolog.logger', ['channel' => 'http_client']);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ private function collectOnClient(TraceableHttpClient $client): array
$errorCount = 0;
$baseInfo = [
'response_headers' => 1,
'retry_count' => 1,
'redirect_count' => 1,
'redirect_url' => 1,
'user_data' => 1,
Expand Down Expand Up @@ -152,6 +153,11 @@ private function collectOnClient(TraceableHttpClient $client): array
$content = [];
}

if (isset($info['retry_count'])) {
$content['retries'] = $info['previous_info'];
unset($info['previous_info']);
}

$debugInfo = array_diff_key($info, $baseInfo);
$info = ['info' => $debugInfo] + array_diff_key($info, $debugInfo) + $content;
unset($traces[$i]['info']); // break PHP reference used by TraceableHttpClient
Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Component/HttpClient/Response/AsyncContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ public function getResponse(): ResponseInterface
public function replaceRequest(string $method, string $url, array $options = []): ResponseInterface
{
$this->info['previous_info'][] = $this->response->getInfo();
if (null !== $onProgress = $options['on_progress'] ?? null) {
$thisInfo = &$this->info;
$options['on_progress'] = static function (int $dlNow, int $dlSize, array $info) use (&$thisInfo, $onProgress) {
$onProgress($dlNow, $dlSize, $thisInfo + $info);
};
}

return $this->response = $this->client->request($method, $url, ['buffer' => false] + $options);
}
Expand Down
7 changes: 7 additions & 0 deletions src/Symfony/Component/HttpClient/Response/AsyncResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ public function __construct(HttpClientInterface $client, string $method, string
{
$this->client = $client;
$this->shouldBuffer = $options['buffer'] ?? true;

if (null !== $onProgress = $options['on_progress'] ?? null) {
$thisInfo = &$this->info;
$options['on_progress'] = static function (int $dlNow, int $dlSize, array $info) use (&$thisInfo, $onProgress) {
$onProgress($dlNow, $dlSize, $thisInfo + $info);
};
}
$this->response = $client->request($method, $url, ['buffer' => false] + $options);
$this->passthru = $passthru;
$this->initializer = static function (self $response) {
Expand Down
4 changes: 1 addition & 3 deletions src/Symfony/Component/HttpClient/RetryableHttpClient.php
10BC0
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ public function request(string $method, string $url, array $options = []): Respo
}
} catch (TransportExceptionInterface $exception) {
// catch TransportExceptionInterface to send it to the strategy
$context->setInfo('retry_count', $retryCount);
}
if (null !== $exception) {
// always retry request that fail to resolve DNS
Expand All @@ -91,8 +90,6 @@ public function request(string $method, string $url, array $options = []): Respo
}
}
} elseif ($chunk->isFirst()) {
$context->setInfo('retry_count', $retryCount);

if (false === $shouldRetry = $this->strategy->shouldRetry($context, null, null)) {
$context->passthru();
yield $chunk;
Expand Down Expand Up @@ -138,6 +135,7 @@ public function request(string $method, string $url, array $options = []): Respo
'delay' => $delay,
]);

$context->setInfo('retry_count', $retryCount);
$context->replaceRequest($method, $url, $options);
$context->pause($delay / 1000);

Expand Down
19 changes: 19 additions & 0 deletions src/Symfony/Component/HttpClient/Tests/AsyncDecoratorTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,4 +263,23 @@ public function request(string $method, string $url, array $options = []): Respo

$this->assertSame('{"documents":[{"id":"\/json\/1"},{"id":"\/json\/2"},{"id":"\/json\/3"}]}', $content);
}

public function testInfoPassToDecorator()
{
$lastInfo = null;
$options = ['on_progress' => function (int $dlNow, int $dlSize, array $info) use (&$lastInfo) {
$lastInfo = $info;
}];
$client = $this->getHttpClient(__FUNCTION__, function (ChunkInterface $chunk, AsyncContext $context) use ($options) {
$context->setInfo('foo', 'test');
$context->getResponse()->cancel();
$context->replaceRequest('GET', 'http://localhost:8057/', $options);
$context->passthru();
});

$client->request('GET', 'http://localhost:8057')->getContent();
$this->assertArrayHasKey('foo', $lastInfo);
$this->assertSame('test', $lastInfo['foo']);
$this->assertArrayHasKey('previous_info', $lastInfo);
}
}
0