8000 feature #38587 [HttpClient] added `extra.trace_content` option to `Tr… · symfony/symfony@72be305 · GitHub
[go: up one dir, main page]

Skip to content

Commit 72be305

Browse files
committed
feature #38587 [HttpClient] added extra.trace_content option to TraceableHttpClient to prevent it from keeping the content in memory (nicolas-grekas)
This PR was merged into the 5.x branch. Discussion ---------- [HttpClient] added `extra.trace_content` option to `TraceableHttpClient` to prevent it from keeping the content in memory | Q | A | ------------- | --- | Branch? | 5.2 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - `TraceableHttpClient` leaks memory by definition. But sometimes, this leak is to important, especially when keeping the response content in memory. This PR adds a new `trace_content` option under `extra` so that consumers can tell the client to not trace the content. This will be ignored when `TraceableHttpClient` is not in use. Commits ------- 61290d5 [HttpClient] added `extra.trace_content` option to `TraceableHttpClient` to prevent it from keeping the content in memory
2 parents c61f657 + 61290d5 commit 72be305

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

src/Symfony/Component/HttpClient/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ CHANGELOG
1111
* added `EventSourceHttpClient` a Server-Sent events stream implementing the [EventSource specification](https://www.w3.org/TR/eventsource/#eventsource)
1212
* added option "extra.curl" to allow setting additional curl options in `CurlHttpClient`
1313
* added `RetryableHttpClient` to automatically retry failed HTTP requests.
14+
* added `extra.trace_content` option to `TraceableHttpClient` to prevent it from keeping the content in memory
1415

1516
5.1.0
1617
-----

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ public function getHeaders(bool $throw = true): array
5252

5353
public function getContent(bool $throw = true): string
5454
{
55+
if (false === $this->content) {
56+
return $this->response->getContent($throw);
57+
}
58+
5559
$this->content = $this->response->getContent(false);
5660

5761
if ($throw) {
@@ -63,6 +67,10 @@ public function getContent(bool $throw = true): string
6367

6468
public function toArray(bool $throw = true): array
6569
{
70+
if (false === $this->content) {
71+
return $this->response->toArray($throw);
72+
}
73+
6674
$this->content = $this->response->toArray(false);
6775

6876
if ($throw) {

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,26 @@ public function testItTracesRequest()
3030
{
3131
$httpClient = $this->getMockBuilder(HttpClientInterface::class)->getMock();
3232
$httpClient
33-
->expects($this->once())
33+
->expects($this->any())
3434
->method('request')
3535
->with(
3636
'GET',
3737
'/foo/bar',
3838
$this->callback(function ($subject) {
3939
$onprogress = $subject['on_progress'];
40-
unset($subject['on_progress']);
40+
unset($subject['on_progress'], $subject['extra']);
4141
$this->assertEquals(['options1' => 'foo'], $subject);
4242

4343
return true;
4444
})
4545
)
4646
->willReturn(MockResponse::fromRequest('GET', '/foo/bar', ['options1' => 'foo'], new MockResponse('hello')))
4747
;
48+
4849
$sut = new TraceableHttpClient($httpClient);
50+
4951
$sut->request('GET', '/foo/bar', ['options1' => 'foo'])->getContent();
52+
5053
$this->assertCount(1, $tracedRequests = $sut->getTracedRequests());
5154
$actualTracedRequest = $tracedRequests[0];
5255
$this->assertEquals([
@@ -56,6 +59,18 @@ public function testItTracesRequest()
5659
'info' => [],
5760
'content' => 'hello',
5861
], $actualTracedRequest);
62+
63+
$sut->request('GET', '/foo/bar', ['options1' => 'foo', 'extra' => ['trace_content' => false]])->getContent();
64+
65+
$this->assertCount(2, $tracedRequests = $sut->getTracedRequests());
66+
$actualTracedRequest = $tracedRequests[1];
67+
$this->assertEquals([
68+
'method' => 'GET',
69+
'url' => '/foo/bar',
70+
'options' => ['options1' => 'foo', 'extra' => ['trace_content' => false]],
71+
'info' => [],
72+
'content' => null,
73+
], $actualTracedRequest);
5974
}
6075

6176
public function testItCollectsInfoOnRealRequest()

src/Symfony/Component/HttpClient/TraceableHttpClient.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ public function request(string $method, string $url, array $options = []): Respo
4949
];
5050
$onProgress = $options['on_progress'] ?? null;
5151

52+
if (false === ($options['extra']['trace_content'] ?? true)) {
53+
unset($content);
54+
$content = false;
55+
}
56+
5257
$options['on_progress'] = function (int $dlNow, int $dlSize, array $info) use (&$traceInfo, $onProgress) {
5358
$traceInfo = $info;
5459

0 commit comments

Comments
 (0)
0