Skip to content

Commit f3cb286

Browse files
committed
bug #62415 [HttpClient] Don't reset unused clients in data collector (HypeMC)
This PR was merged into the 6.4 branch. Discussion ---------- [HttpClient] Don't reset unused clients in data collector | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | - | License | MIT The data collector resets all clients on each request. When multiple scoped clients use the throttling feature, every rate limiter is reset each time, causing locks that were never used to be acquired only to be immediately released. Since most of the time only one or none of the clients is used, this behavior is unnecessary and inefficient. This PR ensures that only clients that were actually used are reset. Commits ------- 44368f4 [HttpClient] Don't reset unused clients in data collector
2 parents 1bc8d80 + 44368f4 commit f3cb286

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ public function lateCollect(): void
6464
$this->data['error_count'] += $errorCount;
6565
$this->data['clients'][$name]['error_count'] += $errorCount;
6666

67-
$client->reset();
67+
if ($traces) {
68+
$client->reset();
69+
}
6870
}
6971
}
7072

src/Symfony/Component/HttpClient/Tests/DataCollector/HttpClientDataCollectorTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\HttpClient\DataCollector\HttpClientDataCollector;
16+
use Symfony\Component\HttpClient\MockHttpClient;
1617
use Symfony\Component\HttpClient\NativeHttpClient;
1718
use Symfony\Component\HttpClient\TraceableHttpClient;
1819
use Symfony\Contracts\HttpClient\Test\TestHttpServer;
@@ -437,4 +438,44 @@ private function httpClientThatHasTracedRequests($tracedRequests): TraceableHttp
437438

438439
return $httpClient;
439440
}
441+
442+
/**
443+
* @dataProvider provideClientIsResetWhenExpectedCases
444+
*/
445+
public function testClientIsResetWhenExpected(\Closure $request, bool $wasReset)
446+
{
447+
$mockHttpClient = new class extends MockHttpClient {
448+
public bool $wasReset = false;
449+
450+
public function reset(): void
451+
{
452+
parent::reset();
453+
454+
$this->wasReset = true;
455+
}
456+
};
457+
458+
$sut = new HttpClientDataCollector();
459+
$sut->registerClient('http_client', $traceableHttpClient = new TraceableHttpClient($mockHttpClient));
460+
$request($traceableHttpClient);
461+
$sut->lateCollect();
462+
463+
$this->assertSame($wasReset, $mockHttpClient->wasReset);
464+
}
465+
466+
public static function provideClientIsResetWhenExpectedCases(): iterable
467+
{
468+
yield [
469+
function (TraceableHttpClient $traceableHttpClient) {
470+
$response = $traceableHttpClient->request('GET', 'http://localhost/');
471+
$response->getContent();
472+
},
473+
true,
474+
];
475+
476+
yield [
477+
fn () => null,
478+
false,
479+
];
480+
}
440481
}

0 commit comments

Comments
 (0)