Skip to content

Commit d3721c5

Browse files
committed
bug #38633 [HttpClient] Fix decorating progress info in AsyncResponse (jderusse)
This PR was squashed before being merged into the 5.x branch. Discussion ---------- [HttpClient] Fix decorating progress info in AsyncResponse | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #38631 | License | MIT | Doc PR | / This PR reverts #38413, and send AsyncContext to onProgress callback. Commits ------- e325f51fe2 [HttpClient] Fix decorating progress info in AsyncResponse
2 parents fe14eb7 + 73b367e commit d3721c5

File tree

5 files changed

+39
-3
lines changed

5 files changed

+39
-3
lines changed

DataCollector/HttpClientDataCollector.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ private function collectOnClient(TraceableHttpClient $client): array
9898
$errorCount = 0;
9999
$baseInfo = [
100100
'response_headers' => 1,
101+
'retry_count' => 1,
101102
'redirect_count' => 1,
102103
'redirect_url' => 1,
103104
'user_data' => 1,
@@ -152,6 +153,11 @@ private function collectOnClient(TraceableHttpClient $client): array
152153
$content = [];
153154
}
154155

156+
if (isset($info['retry_count'])) {
157+
$content['retries'] = $info['previous_info'];
158+
unset($info['previous_info']);
159+
}
160+
155161
$debugInfo = array_diff_key($info, $baseInfo);
156162
$info = ['info' => $debugInfo] + array_diff_key($info, $debugInfo) + $content;
157163
unset($traces[$i]['info']); // break PHP reference used by TraceableHttpClient

Response/AsyncContext.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,12 @@ public function getResponse(): ResponseInterface
151151
public function replaceRequest(string $method, string $url, array $options = []): ResponseInterface
152152
{
153153
$this->info['previous_info'][] = $this->response->getInfo();
154+
if (null !== $onProgress = $options['on_progress'] ?? null) {
155+
$thisInfo = &$this->info;
156+
$options['on_progress'] = static function (int $dlNow, int $dlSize, array $info) use (&$thisInfo, $onProgress) {
157+
$onProgress($dlNow, $dlSize, $thisInfo + $info);
158+
};
159+
}
154160

155161
return $this->response = $this->client->request($method, $url, ['buffer' => false] + $options);
156162
}

Response/AsyncResponse.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ public function __construct(HttpClientInterface $client, string $method, string
4343
{
4444
$this->client = $client;
4545
$this->shouldBuffer = $options['buffer'] ?? true;
46+
47+
if (null !== $onProgress = $options['on_progress'] ?? null) {
48+
$thisInfo = &$this->info;
49+
$options['on_progress'] = static function (int $dlNow, int $dlSize, array $info) use (&$thisInfo, $onProgress) {
50+
$onProgress($dlNow, $dlSize, $thisInfo + $info);
51+
};
52+
}
4653
$this->response = $client->request($method, $url, ['buffer' => false] + $options);
4754
$this->passthru = $passthru;
4855
$this->initializer = static function (self $response) {

RetryableHttpClient.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ public function request(string $method, string $url, array $options = []): Respo
6666
}
6767
} catch (TransportExceptionInterface $exception) {
6868
// catch TransportExceptionInterface to send it to the strategy
69-
$context->setInfo('retry_count', $retryCount);
7069
}
7170
if (null !== $exception) {
7271
// always retry request that fail to resolve DNS
@@ -91,8 +90,6 @@ public function request(string $method, string $url, array $options = []): Respo
9190
}
9291
}
9392
} elseif ($chunk->isFirst()) {
94-
$context->setInfo('retry_count', $retryCount);
95-
9693
if (false === $shouldRetry = $this->strategy->shouldRetry($context, null, null)) {
9794
$context->passthru();
9895
yield $chunk;
@@ -138,6 +135,7 @@ public function request(string $method, string $url, array $options = []): Respo
138135
'delay' => $delay,
139136
]);
140137

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

Tests/AsyncDecoratorTraitTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,4 +263,23 @@ public function request(string $method, string $url, array $options = []): Respo
263263

264264
$this->assertSame('{"documents":[{"id":"\/json\/1"},{"id":"\/json\/2"},{"id":"\/json\/3"}]}', $content);
265265
}
266+
267+
public function testInfoPassToDecorator()
268+
{
269+
$lastInfo = null;
270+
$options = ['on_progress' => function (int $dlNow, int $dlSize, array $info) use (&$lastInfo) {
271+
$lastInfo = $info;
272+
}];
273+
$client = $this->getHttpClient(__FUNCTION__, function (ChunkInterface $chunk, AsyncContext $context) use ($options) {
274+
$context->setInfo('foo', 'test');
275+
$context->getResponse()->cancel();
276+
$context->replaceRequest('GET', 'http://localhost:8057/', $options);
277+
$context->passthru();
278+
});
279+
280+
$client->request('GET', 'http://localhost:8057')->getContent();
281+
$this->assertArrayHasKey('foo', $lastInfo);
282+
$this->assertSame('test', $lastInfo['foo']);
283+
$this->assertArrayHasKey('previous_info', $lastInfo);
284+
}
266285
}

0 commit comments

Comments
 (0)