Skip to content

Commit 9a36fe1

Browse files
staabmclxmstaab
andauthored
Share DNS and Connect between curl requests (#117)
* Share DNS and Connect between curl requests reduces overhead when doing multiple curl requests per php-request. other places which manually invoke `curl_init` can benefit in a similar way by adding such code. `curl_multi_*` will automatically share caches between requests, without the need for additional code. * fix * cs * fix type --------- Co-authored-by: Markus Staab <[email protected]>
1 parent 531bb2a commit 9a36fe1

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/JiraClient.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ class JiraClient
3535
*/
3636
protected \CurlHandle $curl;
3737

38+
/**
39+
* CURL share instance.
40+
*/
41+
protected \CurlShareHandle|\CurlSharePersistentHandle|null $curlShare = null;
42+
3843
/**
3944
* Monolog instance.
4045
*/
@@ -158,6 +163,20 @@ public function curlPrepare(\CurlHandle|bool $ch, array $curl_http_headers, ?str
158163
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->getConfiguration()->getTimeout());
159164
}
160165

166+
if ($this->curlShare === null) {
167+
if (\function_exists('curl_share_init_persistent')) {
168+
$this->curlShare = curl_share_init_persistent([
169+
CURL_LOCK_DATA_DNS,
170+
CURL_LOCK_DATA_CONNECT,
171+
]);
172+
} else {
173+
$this->curlShare = curl_share_init();
174+
curl_share_setopt($share, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
175+
curl_share_setopt($share, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT);
176+
}
177+
}
178+
curl_setopt($ch, CURLOPT_SHARE, $this->curlShare);
179+
161180
return $curl_http_headers;
162181
}
163182

0 commit comments

Comments
 (0)