Skip to content
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

Implement cURL Handle Caching #864

Merged
merged 2 commits into from
Nov 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 36 additions & 34 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class Client
/** @var string */
protected $fingerprint;

/** @var mixed */
protected $curlHandle = null;

public function __construct(int $portNumber = 23517, string $host = 'localhost')
{
$this->fingerprint = $host . ':' . $portNumber;
Expand All @@ -28,6 +31,14 @@ public function __construct(int $portNumber = 23517, string $host = 'localhost')
$this->host = $host;
}

public function __destruct()
{
if ($this->curlHandle) {
curl_close($this->curlHandle);
$this->curlHandle = null;
}
}

public function serverIsAvailable(): bool
{
// purge expired entries from the cache
Expand Down Expand Up @@ -55,10 +66,6 @@ public function performAvailabilityCheck(): bool

static::$cache[$this->fingerprint] = [$success, $expiresAt];
} finally {
if (isset($curlHandle)) {
curl_close($curlHandle);
}

return $success ?? false;
}
}
Expand All @@ -69,25 +76,19 @@ public function send(Request $request): void
return;
}

try {
$curlHandle = $this->getCurlHandleForUrl('get', '');
$curlHandle = $this->getCurlHandleForUrl('get', '');

$curlError = null;
$curlError = null;

curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $request->toJson());
curl_exec($curlHandle);
curl_setopt($this->curlHandle, CURLOPT_POSTFIELDS, $request->toJson());
curl_exec($curlHandle);

if (curl_errno($curlHandle)) {
$curlError = curl_error($curlHandle);
}
if (curl_errno($curlHandle)) {
$curlError = curl_error($curlHandle);
}

if ($curlError) {
// do nothing for now
}
} finally {
if (isset($curlHandle)) {
curl_close($curlHandle);
}
if ($curlError) {
// do nothing for now
}
}

Expand Down Expand Up @@ -131,8 +132,6 @@ public function lockExists(string $lockName): bool
if ($exception instanceof StopExecutionRequested) {
throw $exception;
}
} finally {
curl_close($curlHandle);
}

return false;
Expand All @@ -145,28 +144,31 @@ protected function getCurlHandleForUrl(string $method, string $url)

protected function getCurlHandle(string $method, string $fullUrl)
{
$curlHandle = curl_init();
if (!$this->curlHandle) {
$this->curlHandle = curl_init();
}

curl_setopt($curlHandle, CURLOPT_URL, $fullUrl);
curl_reset($this->curlHandle);
curl_setopt($this->curlHandle, CURLOPT_URL, $fullUrl);

curl_setopt($curlHandle, CURLOPT_HTTPHEADER, array_merge([
curl_setopt($this->curlHandle, CURLOPT_HTTPHEADER, array_merge([
'Accept: application/json',
'Content-Type: application/json',
]));

curl_setopt($curlHandle, CURLOPT_USERAGENT, 'Ray 1.0');
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlHandle, CURLOPT_TIMEOUT, 2);
curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curlHandle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($curlHandle, CURLOPT_ENCODING, '');
curl_setopt($curlHandle, CURLINFO_HEADER_OUT, true);
curl_setopt($curlHandle, CURLOPT_FAILONERROR, true);
curl_setopt($this->curlHandle, CURLOPT_USERAGENT, 'Ray 1.0');
curl_setopt($this->curlHandle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->curlHandle, CURLOPT_TIMEOUT, 2);
curl_setopt($this->curlHandle, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($this->curlHandle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($this->curlHandle, CURLOPT_ENCODING, '');
curl_setopt($this->curlHandle, CURLINFO_HEADER_OUT, true);
curl_setopt($this->curlHandle, CURLOPT_FAILONERROR, true);

if ($method === 'post') {
curl_setopt($curlHandle, CURLOPT_POST, true);
curl_setopt($this->curlHandle, CURLOPT_POST, true);
}

return $curlHandle;
return $this->curlHandle;
}
}