From 083bb50d44fe559dafdc0977d85a8105e2a279b0 Mon Sep 17 00:00:00 2001 From: Olivier Bellone Date: Wed, 14 Nov 2018 16:49:12 +0100 Subject: [PATCH] Reuse curl handle --- lib/HttpClient/CurlClient.php | 74 +++++++++++++++++++++++++++++++---- 1 file changed, 67 insertions(+), 7 deletions(-) diff --git a/lib/HttpClient/CurlClient.php b/lib/HttpClient/CurlClient.php index 3d2474c25b..4431c7a9bd 100644 --- a/lib/HttpClient/CurlClient.php +++ b/lib/HttpClient/CurlClient.php @@ -40,6 +40,10 @@ public static function instance() protected $userAgentInfo; + protected $enablePersistentConnections = true; + + protected $curlHandle = null; + /** * CurlClient constructor. * @@ -60,6 +64,11 @@ public function __construct($defaultOptions = null, $randomGenerator = null) $this->initUserAgentInfo(); } + public function __destruct() + { + $this->closeCurlHandle(); + } + public function initUserAgentInfo() { $curlVersion = curl_version(); @@ -79,6 +88,22 @@ public function getUserAgentInfo() return $this->userAgentInfo; } + /** + * @return boolean + */ + public function getEnablePersistentConnections() + { + return $this->enablePersistentConnections; + } + + /** + * @param boolean $enable + */ + public function setEnablePersistentConnections($enable) + { + $this->enablePersistentConnections = $enable; + } + // USER DEFINED TIMEOUTS const DEFAULT_TIMEOUT = 80; @@ -216,17 +241,19 @@ private function executeRequestWithRetries($opts, $absUrl) $rcode = 0; $errno = 0; - $curl = curl_init(); - curl_setopt_array($curl, $opts); - $rbody = curl_exec($curl); + $this->resetCurlHandle(); + curl_setopt_array($this->curlHandle, $opts); + $rbody = curl_exec($this->curlHandle); if ($rbody === false) { - $errno = curl_errno($curl); - $message = curl_error($curl); + $errno = curl_errno($this->curlHandle); + $message = curl_error($this->curlHandle); } else { - $rcode = curl_getinfo($curl, CURLINFO_HTTP_CODE); + $rcode = curl_getinfo($this->curlHandle, CURLINFO_HTTP_CODE); + } + if (!$this->getEnablePersistentConnections()) { + $this->closeCurlHandle(); } - curl_close($curl); if ($this->shouldRetry($errno, $rcode, $numRetries)) { $numRetries += 1; @@ -338,4 +365,37 @@ private function sleepTime($numRetries) return $sleepSeconds; } + + /** + * Initializes the curl handle. If already initialized, the handle is closed first. + */ + private function initCurlHandle() + { + $this->closeCurlHandle(); + $this->curlHandle = curl_init(); + } + + /** + * Closes the curl handle if initialized. Do nothing if already closed. + */ + private function closeCurlHandle() + { + if (!is_null($this->curlHandle)) { + curl_close($this->curlHandle); + $this->curlHandle = null; + } + } + + /** + * Resets the curl handle. If the handle is not already initialized, or if persistent + * connections are disabled, the handle is reinitialized instead. + */ + private function resetCurlHandle() + { + if (!is_null($this->curlHandle) && $this->getEnablePersistentConnections()) { + curl_reset($this->curlHandle); + } else { + $this->initCurlHandle(); + } + } }