Skip to content

Commit 839d9d4

Browse files
[HttpClient] Improve memory consumption
1 parent 02a2e8b commit 839d9d4

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

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
}

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)