Skip to content

[HttpClient] Fix setting CURLMOPT_MAXCONNECTS #58278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Comment on lines +121 to +137
Copy link
Contributor Author

@HypeMC HypeMC Sep 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Backported from later versions of Symfony. This is needed since the PHP built-in server doesn't support persistent connections.


steps:
- name: Checkout
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/HttpClient/Internal/CurlClientState.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
30 changes: 30 additions & 0 deletions src/Symfony/Component/HttpClient/Tests/CurlHttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

echo 'Success';
Loading