10000 minor #60241 [HttpClient] Improve memory consumption (nicolas-grekas) · symfony/symfony@29da4f5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 29da4f5

Browse files
minor #60241 [HttpClient] Improve memory consumption (nicolas-grekas)
This PR was merged into the 7.3 branch. Discussion ---------- [HttpClient] Improve memory consumption | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | no | New feature? | no | Deprecations? | no | Issues | Partially #60037 | License | MIT On the CLI, even if the profiler is not enabled by default, the underlying infrastructure is still in place. In this PR, I'm targeting TraceableHttpClient, which collects requests and responses for nothing. To fix this, I'm adding an "enabled" flag on TraceableHttpClient, so that we can enable the tracing only when the profiler is enabled. In addition, I'm augmenting option `extra.trace_content` so that when disabled, we don't collect request's bodies, which can be huge when uploading. And last but not least, I'm fixing the implementation of curl's debug info collection, which currently relies on allocating new strings all the time instead of reusing the already created zval when debug info didn't change. Commits ------- 0e865e6 [HttpClient] Improve memory consumption
2 parents fe94cfb + 0e865e6 commit 29da4f5

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

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/TraceableHttpClient.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function request(string $method, string $url, array $options = []): Respo
3939
{
4040
$content = null;
4141
$traceInfo = [];
42-
$this->tracedRequests[] = [
42+
$tracedRequest = [
4343
'method' => $method,
4444
'url' => $url,
4545
'options' => $options,
@@ -51,7 +51,9 @@ public function request(string $method, string $url, array $options = []): Respo
5151
if (false === ($options['extra']['trace_content'] ?? true)) {
5252
unset($content);
5353
$content = false;
54+
unset($tracedRequest['options']['body'], $tracedRequest['options']['json']);
5455
}
56+
$this->tracedRequests[] = $tracedRequest;
5557

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

0 commit comments

Comments
 (0)
0