Skip to content

Commit 58d3dc6

Browse files
committed
[HttpClient] Fix setting CURLMOPT_MAXCONNECTS
1 parent f09ba83 commit 58d3dc6

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

Internal/CurlClientState.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ public function __construct(int $maxHostConnections, int $maxPendingPushes)
5252
if (\defined('CURLPIPE_MULTIPLEX')) {
5353
curl_multi_setopt($this->handle, \CURLMOPT_PIPELINING, \CURLPIPE_MULTIPLEX);
5454
}
55-
if (\defined('CURLMOPT_MAX_HOST_CONNECTIONS')) {
56-
$maxHostConnections = curl_multi_setopt($this->handle, \CURLMOPT_MAX_HOST_CONNECTIONS, 0 < $maxHostConnections ? $maxHostConnections : \PHP_INT_MAX) ? 0 : $maxHostConnections;
55+
if (\defined('CURLMOPT_MAX_HOST_CONNECTIONS') && 0 < $maxHostConnections) {
56+
$maxHostConnections = curl_multi_setopt($this->handle, \CURLMOPT_MAX_HOST_CONNECTIONS, $maxHostConnections) ? 4294967295 : $maxHostConnections;
5757
}
5858
if (\defined('CURLMOPT_MAXCONNECTS') && 0 < $maxHostConnections) {
5959
curl_multi_setopt($this->handle, \CURLMOPT_MAXCONNECTS, $maxHostConnections);

Tests/CurlHttpClientTest.php

+30
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,34 @@ public function testKeepAuthorizationHeaderOnRedirectToSameHostWithConfiguredHos
144144
$this->assertSame(200, $response->getStatusCode());
145145
$this->assertSame('/302', $response->toArray()['REQUEST_URI'] ?? null);
146146
}
147+
148+
/**
149+
* @group integration
150+
*/
151+
public function testMaxConnections()
152+
{
153+
foreach ($ports = [80, 8681, 8682, 8683, 8684] as $port) {
154+
if (!($fp = @fsockopen('localhost', $port, $errorCode, $errorMessage, 2))) {
155+
self::markTestSkipped('FrankenPHP is not running');
156+
}
157+
fclose($fp);
158+
}
159+
160+
$httpClient = $this->getHttpClient(__FUNCTION__);
161+
162+
$expectedResults = [
163+
[false, false, false, false, false],
164+
[true, true, true, true, true],
165+
[true, true, true, true, true],
166+
];
167+
168+
foreach ($expectedResults as $expectedResult) {
169+
foreach ($ports as $i => $port) {
170+
$response = $httpClient->request('GET', \sprintf('http://localhost:%s/http-client', $port));
171+
$response->getContent();
172+
173+
self::assertSame($expectedResult[$i], str_contains($response->getInfo('debug'), 'Re-using existing connection'));
174+
}
175+
}
176+
}
147177
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
echo 'Success';

0 commit comments

Comments
 (0)