@@ -38,6 +38,7 @@ class SocketClient
38
38
protected string $ e2eEncryptionKey = '' ;
39
39
protected string $ paramsMethod = 'path ' ;
40
40
protected ?string $ loggerFile = null ;
41
+ protected ?bool $ enableCurlShare = true ;
41
42
protected bool $ curlForbidReuse = false ; // 是否禁用 curl 复用
42
43
private bool $ isInitialized = false ;
43
44
@@ -72,9 +73,20 @@ public static function fromUri(string $uri): self
72
73
);
73
74
}
74
75
75
- private function initHandles ( ): void
76
+ public function setEnableCurlShare (? bool $ enableCurlShare ): void
76
77
{
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 ) {
78
90
// 初始化共享句柄
79
91
$ this ->shareHandle = curl_share_init ();
80
92
curl_share_setopt ($ this ->shareHandle , CURLSHOPT_SHARE , CURL_LOCK_DATA_DNS );
@@ -202,7 +214,8 @@ protected function buildUrl(string $clientId): string
202
214
*/
203
215
public function send (string $ message , string $ clientId )
204
216
{
205
- $ this ->initHandles ();
217
+ $ this ->initShareHandles ();
218
+
206
219
$ url = $ this ->buildUrl ($ clientId );
207
220
208
221
[$ message , $ contentType , $ headers ] = $ this ->buildPayload ($ message , $ clientId );
@@ -354,14 +367,21 @@ public function resetConnection(): void
354
367
}
355
368
}
356
369
357
- public function __destruct ()
370
+ private function closeHandles (): void
358
371
{
359
372
if ($ this ->curlHandle ) {
360
373
curl_close ($ this ->curlHandle );
374
+ $ this ->curlHandle = null ;
361
375
}
362
376
363
377
if ($ this ->shareHandle ) {
364
378
curl_share_close ($ this ->shareHandle );
379
+ $ this ->shareHandle = null ;
365
380
}
366
381
}
382
+
383
+ public function __destruct ()
384
+ {
385
+ $ this ->closeHandles ();
386
+ }
367
387
}
0 commit comments