Skip to content

Commit eeb7dde

Browse files
committed
feat(transport): use share handle
1 parent 5eea18c commit eeb7dde

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

src/HttpClient/HttpClient.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,44 @@ class HttpClient implements HttpClientInterface
2222
*/
2323
protected $sdkVersion;
2424

25+
/**
26+
* Either a persistent share handle or a regular share handle, or null if no share handle can be obtained.
27+
*
28+
* @var object|resource|null
29+
*/
30+
private $shareHandle;
31+
2532
public function __construct(string $sdkIdentifier, string $sdkVersion)
2633
{
2734
$this->sdkIdentifier = $sdkIdentifier;
2835
$this->sdkVersion = $sdkVersion;
36+
if (\function_exists('curl_share_init_persistent')) {
37+
$shareOptions = [\CURL_LOCK_DATA_DNS, \CURL_LOCK_DATA_CONNECT];
38+
if (\defined('CURL_LOCK_DATA_SSL_SESSION')) {
39+
$shareOptions[] = \CURL_LOCK_DATA_SSL_SESSION;
40+
}
41+
try {
42+
$this->shareHandle = curl_share_init_persistent($shareOptions);
43+
} catch (\Throwable $throwable) {
44+
// don't crash if the share handle cannot be created
45+
}
46+
}
47+
48+
// If the persistent share handle cannot be created or doesn't exist
49+
if ($this->shareHandle === null) {
50+
try {
51+
$this->shareHandle = curl_share_init();
52+
curl_share_setopt($this->shareHandle, \CURLOPT_SHARE, \CURL_LOCK_DATA_DNS);
53+
if (\defined('CURL_LOCK_DATA_CONNECT')) {
54+
curl_share_setopt($this->shareHandle, \CURLOPT_SHARE, \CURL_LOCK_DATA_CONNECT);
55+
}
56+
if (\defined('CURL_LOCK_DATA_SSL_SESSION')) {
57+
curl_share_setopt($this->shareHandle, \CURLOPT_SHARE, \CURL_LOCK_DATA_SSL_SESSION);
58+
}
59+
} catch (\Throwable $throwable) {
60+
// don't crash if the share handle cannot be created
61+
}
62+
}
2963
}
3064

3165
public function sendRequest(Request $request, Options $options): Response
@@ -72,6 +106,9 @@ public function sendRequest(Request $request, Options $options): Response
72106
curl_setopt($curlHandle, \CURLOPT_RETURNTRANSFER, true);
73107
curl_setopt($curlHandle, \CURLOPT_HEADERFUNCTION, $responseHeaderCallback);
74108
curl_setopt($curlHandle, \CURLOPT_HTTP_VERSION, \CURL_HTTP_VERSION_1_1);
109+
if ($this->shareHandle !== null) {
110+
curl_setopt($curlHandle, \CURLOPT_SHARE, $this->shareHandle);
111+
}
75112

76113
$httpSslVerifyPeer = $options->getHttpSslVerifyPeer();
77114
if (!$httpSslVerifyPeer) {

0 commit comments

Comments
 (0)