Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit eedd611

Browse files
committed
update: 支持控制 curl 共享激活状态
1 parent 8ab0652 commit eedd611

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

src/SocketClient.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class SocketClient
3838
protected string $e2eEncryptionKey = '';
3939
protected string $paramsMethod = 'path';
4040
protected ?string $loggerFile = null;
41+
protected ?bool $enableCurlShare = true;
4142
protected bool $curlForbidReuse = false; // 是否禁用 curl 复用
4243
private bool $isInitialized = false;
4344

@@ -72,9 +73,20 @@ public static function fromUri(string $uri): self
7273
);
7374
}
7475

75-
private function initHandles(): void
76+
public function setEnableCurlShare(?bool $enableCurlShare): void
7677
{
77-
if (!$this->isInitialized) {
78+
$this->enableCurlShare = $enableCurlShare;
79+
80+
// 如果已经初始化,则关闭连接
81+
if ($this->isInitialized && !$enableCurlShare) {
82+
$this->isInitialized = false;
83+
$this->closeHandles();
84+
}
85+
}
86+
87+
private function initShareHandles(): void
88+
{
89+
if (!$this->isInitialized && $this->enableCurlShare) {
7890
// 初始化共享句柄
7991
$this->shareHandle = curl_share_init();
8092
curl_share_setopt($this->shareHandle, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
@@ -202,7 +214,8 @@ protected function buildUrl(string $clientId): string
202214
*/
203215
public function send(string $message, string $clientId)
204216
{
205-
$this->initHandles();
217+
$this->initShareHandles();
218+
206219
$url = $this->buildUrl($clientId);
207220

208221
[$message, $contentType, $headers] = $this->buildPayload($message, $clientId);
@@ -354,14 +367,21 @@ public function resetConnection(): void
354367
}
355368
}
356369

357-
public function __destruct()
370+
private function closeHandles(): void
358371
{
359372
if ($this->curlHandle) {
360373
curl_close($this->curlHandle);
374+
$this->curlHandle = null;
361375
}
362376

363377
if ($this->shareHandle) {
364378
curl_share_close($this->shareHandle);
379+
$this->shareHandle = null;
365380
}
366381
}
382+
383+
public function __destruct()
384+
{
385+
$this->closeHandles();
386+
}
367387
}

0 commit comments

Comments
 (0)