From 4f9b5decd96a9d0af2749ad83e6ce1f4a6608177 Mon Sep 17 00:00:00 2001 From: HypeMC Date: Mon, 16 Sep 2024 14:08:49 +0200 Subject: [PATCH] [HttpClient] Fix setting CURLMOPT_MAXCONNECTS --- .github/workflows/integration-tests.yml | 17 +++++++++++ .../HttpClient/Internal/CurlClientState.php | 4 +-- .../HttpClient/Tests/CurlHttpClientTest.php | 30 +++++++++++++++++++ .../Fixtures/response-functional/index.php | 12 ++++++++ 4 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 src/Symfony/Component/HttpClient/Tests/Fixtures/response-functional/index.php diff --git a/.github/workflows/integration-tests.yml b/.github/workflows/integration-tests.yml index 25efdc118e4a7..f60355cff86c8 100644 --- a/.github/workflows/integration-tests.yml +++ b/.github/workflows/integration-tests.yml @@ -118,6 +118,23 @@ jobs: KAFKA_CFG_LISTENERS: 'PLAINTEXT://:9092' KAFKA_CFG_ZOOKEEPER_CONNECT: 'zookeeper:2181' options: --name=kafka + frankenphp: + image: dunglas/frankenphp:1.1.0 + ports: + - 80:80 + - 8681:81 + - 8682:82 + - 8683:83 + - 8684:84 + volumes: + - ${{ github.workspace }}:/symfony + env: + SERVER_NAME: 'http://localhost http://localhost:81 http://localhost:82 http://localhost:83 http://localhost:84' + CADDY_SERVER_EXTRA_DIRECTIVES: | + route /http-client* { + root * /symfony/src/Symfony/Component/HttpClient/Tests/Fixtures/response-functional/ + php_server + } steps: - name: Checkout diff --git a/src/Symfony/Component/HttpClient/Internal/CurlClientState.php b/src/Symfony/Component/HttpClient/Internal/CurlClientState.php index 80473fee07021..eca3d5add4a8e 100644 --- a/src/Symfony/Component/HttpClient/Internal/CurlClientState.php +++ b/src/Symfony/Component/HttpClient/Internal/CurlClientState.php @@ -52,8 +52,8 @@ public function __construct(int $maxHostConnections, int $maxPendingPushes) if (\defined('CURLPIPE_MULTIPLEX')) { curl_multi_setopt($this->handle, \CURLMOPT_PIPELINING, \CURLPIPE_MULTIPLEX); } - if (\defined('CURLMOPT_MAX_HOST_CONNECTIONS')) { - $maxHostConnections = curl_multi_setopt($this->handle, \CURLMOPT_MAX_HOST_CONNECTIONS, 0 < $maxHostConnections ? $maxHostConnections : \PHP_INT_MAX) ? 0 : $maxHostConnections; + if (\defined('CURLMOPT_MAX_HOST_CONNECTIONS') && 0 < $maxHostConnections) { + $maxHostConnections = curl_multi_setopt($this->handle, \CURLMOPT_MAX_HOST_CONNECTIONS, $maxHostConnections) ? 4294967295 : $maxHostConnections; } if (\defined('CURLMOPT_MAXCONNECTS') && 0 < $maxHostConnections) { curl_multi_setopt($this->handle, \CURLMOPT_MAXCONNECTS, $maxHostConnections); diff --git a/src/Symfony/Component/HttpClient/Tests/CurlHttpClientTest.php b/src/Symfony/Component/HttpClient/Tests/CurlHttpClientTest.php index 9ea976271b5ae..d8165705ca111 100644 --- a/src/Symfony/Component/HttpClient/Tests/CurlHttpClientTest.php +++ b/src/Symfony/Component/HttpClient/Tests/CurlHttpClientTest.php @@ -144,4 +144,34 @@ public function testKeepAuthorizationHeaderOnRedirectToSameHostWithConfiguredHos $this->assertSame(200, $response->getStatusCode()); $this->assertSame('/302', $response->toArray()['REQUEST_URI'] ?? null); } + + /** + * @group integration + */ + public function testMaxConnections() + { + foreach ($ports = [80, 8681, 8682, 8683, 8684] as $port) { + if (!($fp = @fsockopen('localhost', $port, $errorCode, $errorMessage, 2))) { + self::markTestSkipped('FrankenPHP is not running'); + } + fclose($fp); + } + + $httpClient = $this->getHttpClient(__FUNCTION__); + + $expectedResults = [ + [false, false, false, false, false], + [true, true, true, true, true], + [true, true, true, true, true], + ]; + + foreach ($expectedResults as $expectedResult) { + foreach ($ports as $i => $port) { + $response = $httpClient->request('GET', \sprintf('http://localhost:%s/http-client', $port)); + $response->getContent(); + + self::assertSame($expectedResult[$i], str_contains($response->getInfo('debug'), 'Re-using existing connection')); + } + } + } } diff --git a/src/Symfony/Component/HttpClient/Tests/Fixtures/response-functional/index.php b/src/Symfony/Component/HttpClient/Tests/Fixtures/response-functional/index.php new file mode 100644 index 0000000000000..7a8076aaa8992 --- /dev/null +++ b/src/Symfony/Component/HttpClient/Tests/Fixtures/response-functional/index.php @@ -0,0 +1,12 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +echo 'Success';