Skip to content

Commit d2bcf96

Browse files
[HttpClient] Improve memory consumption
1 parent 904df78 commit d2bcf96

File tree

5 files changed

+24
-8
lines changed

5 files changed

+24
-8
lines changed

src/Symfony/Component/HttpClient/DataCollector/HttpClientDataCollector.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ final class HttpClientDataCollector extends DataCollector implements LateDataCol
3636
public function registerClient(string $name, TraceableHttpClient $client): void
3737
{
3838
$this->clients[$name] = $client;
39+
$client->enabled = true;
3940
}
4041

4142
public function collect(Request $request, Response $response, ?\Throwable $exception = null): void

src/Symfony/Component/HttpClient/DependencyInjection/HttpClientPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function process(ContainerBuilder $container): void
2727

2828
foreach ($container->findTaggedServiceIds('http_client.client') as $id => $tags) {
2929
$container->register('.debug.'.$id, TraceableHttpClient::class)
30-
->setArguments([new Reference('.debug.'.$id.'.inner'), new Reference('debug.stopwatch', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)])
30+
->setArguments([new Reference('.debug.'.$id.'.inner'), new Reference('debug.stopwatch', ContainerInterface::IGNORE_ON_INVALID_REFERENCE), '%kernel.runtime_mode.web%'])
3131
->addTag('kernel.reset', ['method' => 'reset'])
3232
->setDecoratedService($id, null, 5);
3333
$container->getDefinition('data_collector.http_client')

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,14 @@ public function __construct(
126126
curl_setopt($ch, \CURLOPT_NOPROGRESS, false);
127127
curl_setopt($ch, \CURLOPT_PROGRESSFUNCTION, static function ($ch, $dlSize, $dlNow) use ($onProgress, &$info, $url, $multi, $debugBuffer) {
128128
try {
129+
$info['debug'] ??= '';
129130
rewind($debugBuffer);
130-
$debug = ['debug' => stream_get_contents($debugBuffer)];
131-
$onProgress($dlNow, $dlSize, $url + curl_getinfo($ch) + $info + $debug);
131+
if (fstat($debugBuffer)['size']) {
132+
$info['debug'] .= stream_get_contents($debugBuffer);
133+
rewind($debugBuffer);
134+
ftruncate($debugBuffer, 0);
135+
}
136+
$onProgress($dlNow, $dlSize, $url + curl_getinfo($ch) + $info);
132137
} catch (\Throwable $e) {
133138
$multi->handlesActivity[(int) $ch][] = null;
134139
$multi->handlesActivity[(int) $ch][] = $e;
@@ -209,14 +214,17 @@ public function getInfo(?string $type = null): mixed
209214
$info['starttransfer_time'] = 0.0;
210215
}
211216

217+
$info['debug'] ??= '';
212218
rewind($this->debugBuffer);
213-
$info['debug'] = stream_get_contents($this->debugBuffer);
219+
if (fstat($this->debugBuffer)['size']) {
220+
$info['debug'] .= stream_get_contents($this->debugBuffer);
221+
rewind($this->debugBuffer);
222+
ftruncate($this->debugBuffer, 0);
223+
}
214224
$waitFor = curl_getinfo($this->handle, \CURLINFO_PRIVATE);
215225

216226
if ('H' !== $waitFor[0] && 'C' !== $waitFor[0]) {
217227
curl_setopt($this->handle, \CURLOPT_VERBOSE, false);
218-
rewind($this->debugBuffer);
219-
ftruncate($this->debugBuffer, 0);
220228
$this->finalInfo = $info;
221229
}
222230
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class TraceableResponse implements ResponseInterface, StreamableInterface
3434
public function __construct(
3535
private HttpClientInterface $client,
3636
private ResponseInterface $response,
37-
private mixed &$content,
37+
private mixed &$content = false,
3838
private ?StopwatchEvent $event = null,
3939
) {
4040
}

src/Symfony/Component/HttpClient/TraceableHttpClient.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,20 @@ final class TraceableHttpClient implements HttpClientInterface, ResetInterface,
3131
public function __construct(
3232
private HttpClientInterface $client,
3333
private ?Stopwatch $stopwatch = null,
34+
public bool $enabled = true,
3435
) {
3536
$this->tracedRequests = new \ArrayObject();
3637
}
3738

3839
public function request(string $method, string $url, array $options = []): ResponseInterface
3940
{
41+
if (!$this->enabled) {
42+
return new TraceableResponse($this->client, $this->client->request($method, $url, $options));
43+
}
44+
4045
$content = null;
4146
$traceInfo = [];
42-
$this->tracedRequests[] = [
47+
$tracedRequest = [
4348
'method' => $method,
4449
'url' => $url,
4550
'options' => $options,
@@ -51,7 +56,9 @@ public function request(string $method, string $url, array $options = []): Respo
5156
if (false === ($options['extra']['trace_content'] ?? true)) {
5257
unset($content);
5358
$content = false;
59+
unset($tracedRequest['options']['body'], $tracedRequest['options']['json']);
5460
}
61+
$this->tracedRequests[] = $tracedRequest;
5562

5663
$options['on_progress'] = function (int $dlNow, int $dlSize, array $info) use (&$traceInfo, $onProgress) {
5764
$traceInfo = $info;

0 commit comments

Comments
 (0)