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

Conversation

HypeMC
Copy link
Contributor

@HypeMC HypeMC commented Sep 16, 2024

Q A
Branch? 5.4
Bug fix? yes
New feature? no
Deprecations? no
Issues -
License MIT

An alternative approach for #58270, see #58270 (comment).

Comment on lines +121 to +137
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
}
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.

$maxHostConnections = curl_multi_setopt($this->handle, \CURLMOPT_MAX_HOST_CONNECTIONS, 0 < $maxHostConnections ? $maxHostConnections : \PHP_INT_MAX) ? 0 : $maxHostConnections;
$maxHostConnections = curl_multi_setopt($this->handle, \CURLMOPT_MAX_HOST_CONNECTIONS, 0 < $maxHostConnections ? $maxHostConnections : \PHP_INT_MAX) ? 4294967295 : $maxHostConnections;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

As @nicolas-grekas mentioned, zero doesn't mean unlimited. Instead, it sets the default behavior to four times the number of added handlers. This can be seen in the code:

  unsigned int maxconnects = !data->multi->maxconnects ?
    data->multi->num_easy * 4: data->multi->maxconnects;

Also, since the variable is an int, PHP_INT_MAX cannot be used, as it’s too large on 64-bit systems.

Copy link
Member

Choose a reason for hiding this comment

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

Really? curl has fixed size for long? Do we know if the number is signed? Aka 2147483647 would then be the max?

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.

@nicolas-grekas The variable is declared as unsigned int maxconnects. Also, I've tested it, 4294967295 works, 4294967296 doesn't any more, so it's definitely unsigned. The test fails otherwise.

@@ -53,7 +53,7 @@ public function __construct(int $maxHostConnections, int $maxPendingPushes)
curl_multi_setopt($this->handle, \CURLMOPT_PIPELINING, \CURLPIPE_MULTIPLEX);
}
if (\defined('CURLMOPT_MAX_HOST_CONNECTIONS')) {
Copy link
Member

@nicolas-grekas nicolas-grekas Sep 16, 2024

Choose a reason for hiding this comment

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

let's keep the defaults when maxHostConnections is not set?

Suggested change
if (\defined('CURLMOPT_MAX_HOST_CONNECTIONS')) {
if (\defined('CURLMOPT_MAX_HOST_CONNECTIONS') && 0 < $maxHostConnections) {
$maxHostConnections = curl_multi_setopt($this->handle, \CURLMOPT_MAX_HOST_CONNECTIONS, $maxHostConnections) ? 2147483647 : $maxHostConnections;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@HypeMC HypeMC force-pushed the fix-setting-curlmopt-maxconnects branch from 8fe2f2f to 4f9b5de Compare September 16, 2024 14:06
@nicolas-grekas
Copy link
Member

Thank you @HypeMC.

@nicolas-grekas nicolas-grekas merged commit c8d5d4d into symfony:5.4 Sep 16, 2024
10 of 12 checks passed
@HypeMC HypeMC deleted the fix-setting-curlmopt-maxconnects branch September 16, 2024 14:10
This was referenced Sep 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants