-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[HttpClient] Add support of the persistent cURL handles #62751
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
base: 8.1
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1961,6 +1961,10 @@ private function addHttpClientSection(ArrayNodeDefinition $rootNode, callable $e | |||||
| ->scalarNode('http_version') | ||||||
| ->info('The default HTTP version, typically 1.1 or 2.0, leave to null for the best version.') | ||||||
| ->end() | ||||||
| ->booleanNode('allow_persistent_handles') | ||||||
| ->info('Whether to allow persistent handles for cURL introduced PHP 8.5.') | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| ->defaultFalse() | ||||||
| ->end() | ||||||
| ->arrayNode('resolve') | ||||||
| ->info('Associative array: domain => IP.') | ||||||
| ->useAttributeAsKey('host') | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,6 +1,11 @@ | ||||||
| CHANGELOG | ||||||
| ========= | ||||||
|
|
||||||
| 8.1 | ||||||
| --- | ||||||
|
|
||||||
| * Add option `allow_persistent_handles` to `CurlHttpClient` to control the use of persistent connections introduced in PHP 8.5 | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| 8.0 | ||||||
| --- | ||||||
|
|
||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -42,10 +42,11 @@ final class CurlHttpClient implements HttpClientInterface, LoggerAwareInterface, | |||||
|
|
||||||
| private array $defaultOptions = self::OPTIONS_DEFAULTS + [ | ||||||
| 'auth_ntlm' => null, // array|string - an array containing the username as first value, and optionally the | ||||||
| // password as the second one; or string like username:password - enabling NTLM auth | ||||||
| // password as the second one; or string like username:password - enabling NTLM auth | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this reformatting was suggested by php-cs-fixer, not by me
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and we're the ones able to spot false-positives ;) |
||||||
| 'extra' => [ | ||||||
| 'curl' => [], // A list of extra curl options indexed by their corresponding CURLOPT_* | ||||||
| ], | ||||||
| 'allow_persistent_handles' => false, | ||||||
| ]; | ||||||
| private static array $emptyDefaults = self::OPTIONS_DEFAULTS + ['auth_ntlm' => null]; | ||||||
|
|
||||||
|
|
@@ -75,7 +76,7 @@ public function __construct(array $defaultOptions = [], int $maxHostConnections | |||||
| [, $this->defaultOptions] = self::prepareRequest(null, null, $defaultOptions, $this->defaultOptions); | ||||||
| } | ||||||
|
|
||||||
| $this->multi = new CurlClientState($maxHostConnections, $maxPendingPushes); | ||||||
| $this->multi = new CurlClientState($maxHostConnections, $maxPendingPushes, $defaultOptions['allow_persistent_handles'] ?? false); | ||||||
| } | ||||||
|
|
||||||
| public function setLogger(LoggerInterface $logger): void | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -24,7 +24,7 @@ | |||||
| final class CurlClientState extends ClientState | ||||||
| { | ||||||
| public ?\CurlMultiHandle $handle; | ||||||
| public ?\CurlShareHandle $share; | ||||||
| public \CurlShareHandle|\CurlSharePersistentHandle|null $share; | ||||||
| public bool $performing = false; | ||||||
|
|
||||||
| /** @var PushedResponse[] */ | ||||||
|
|
@@ -40,12 +40,18 @@ final class CurlClientState extends ClientState | |||||
| public function __construct( | ||||||
| private int $maxHostConnections, | ||||||
| private int $maxPendingPushes, | ||||||
| private bool $allowPersistentHandles = false, | ||||||
| ) { | ||||||
| self::$curlVersion ??= curl_version(); | ||||||
| $this->dnsCache = new DnsCache(); | ||||||
|
|
||||||
| // handle and share are initialized lazily in __get() | ||||||
| unset($this->handle, $this->share); | ||||||
|
|
||||||
| if ($this->allowPersistentHandles && \PHP_VERSION_ID < 80500) { | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure this is useful |
||||||
| $this->allowPersistentHandles = false; | ||||||
| @trigger_error('Upgrade PHP to version 8.5 to enable persistent handles.', \E_USER_WARNING); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| public function reset(): void | ||||||
|
|
@@ -59,18 +65,24 @@ public function reset(): void | |||||
| $this->dnsCache->evictions = $this->dnsCache->evictions ?: $this->dnsCache->removals; | ||||||
| $this->dnsCache->removals = $this->dnsCache->hostnames = []; | ||||||
|
|
||||||
| unset($this->share); | ||||||
| if (!$this->allowPersistentHandles) { | ||||||
| unset($this->share); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| public function __get(string $name): mixed | ||||||
| { | ||||||
| if ('share' === $name) { | ||||||
| $this->share = curl_share_init(); | ||||||
|
|
||||||
| curl_share_setopt($this->share, \CURLSHOPT_SHARE, \CURL_LOCK_DATA_DNS); | ||||||
| curl_share_setopt($this->share, \CURLSHOPT_SHARE, \CURL_LOCK_DATA_SSL_SESSION); | ||||||
|
|
||||||
| if (\defined('CURL_LOCK_DATA_CONNECT')) { | ||||||
| if ($this->allowPersistentHandles && (\PHP_VERSION_ID >= 80500)) { | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| $this->share = curl_share_init_persistent([ | ||||||
| \CURL_LOCK_DATA_DNS, | ||||||
| \CURL_LOCK_DATA_SSL_SESSION, | ||||||
| \CURL_LOCK_DATA_CONNECT, | ||||||
| ]); | ||||||
| } else { | ||||||
| $this->share = curl_share_init(); | ||||||
| curl_share_setopt($this->share, \CURLSHOPT_SHARE, \CURL_LOCK_DATA_DNS); | ||||||
| curl_share_setopt($this->share, \CURLSHOPT_SHARE, \CURL_LOCK_DATA_SSL_SESSION); | ||||||
| curl_share_setopt($this->share, \CURLSHOPT_SHARE, \CURL_LOCK_DATA_CONNECT); | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be at the same level as max_host_connections, this cannot be configured as a client option
about the naming, what about this?