10000 Collect retries info · symfony/symfony@51440f2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 51440f2

Browse files
committed
Collect retries info
1 parent 277403e commit 51440f2

File tree

8 files changed

+40
-17
lines changed

8 files changed

+40
-17
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2086,7 +2086,7 @@ private function registerRetryableHttpClient(array $options, string $name, Conta
20862086

20872087
$container
20882088
->register($name.'.retryable', RetryableHttpClient::class)
2089-
->setDecoratedService($name, null, -10) // lower priority than TraceableHttpClient
2089+
->setDecoratedService($name)
20902090
->setArguments([new Reference($name.'.retryable.inner'), $retryStrategy, $options['max_retries'], new Reference('logger')])
20912091
->addTag('monolog.logger', ['channel' => 'http_client']);
20922092
}

src/Symfony/Component/HttpClient/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

src/Symfony/Component/HttpClient/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
}

src/Symfony/Component/HttpClient/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) {

src/Symfony/Component/HttpClient/Response/TraceableResponse.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,6 @@ public static function stream(HttpClientInterface $client, iterable $responses,
132132
}
133133

134134
foreach ($client->stream($wrappedResponses, $timeout) as $r => $chunk) {
135-
if (false !== $traceableMap[$r]->content && null === $chunk->getError()) {
136-
$traceableMap[$r]->content .= $chunk->getContent();
137-
}
138135
yield $traceableMap A3E2 [$r] => $chunk;
139136
}
140137
}

src/Symfony/Component/HttpClient/RetryableHttpClient.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ public function request(string $method, string $url, array $options = []): Respo
123123
$context->pause($delay / 1000);
124124

125125
if ($retryCount >= $this->maxRetries) {
126+
$context->setInfo('retry_count', $retryCount);
126127
$context->passthru();
127128
}
128129
});

src/Symfony/Component/HttpClient/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 testInfoPa F438 ssToDecorator()
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
}

src/Symfony/Component/HttpClient/Tests/TraceableHttpClientTest.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,4 @@ public function testStream()
115115
$this->assertGreaterThan(1, \count($chunks));
116116
$this->assertSame('Symfony is awesome!', implode('', $chunks));
117117
}
118-
119-
public function testStreamCollectContent()
120-
{
121-
$sut = new TraceableHttpClient(new NativeHttpClient());
122-
$response = $sut->request('GET', 'http://localhost:8057/json/1');
123-
foreach ($sut->stream($response) as $chunk) {
124-
// no-op
125-
}
126-
127-
$this->assertCount(1, $tracedRequests = $sut->getTracedRequests());
128-
$actualTracedRequest = $tracedRequests[0];
129-
$this->assertSame('{"title":"\\/json\\/1"}', $actualTracedRequest['content']);
130-
}
131118
}

0 commit comments

Comments
 (0)
0