Skip to content

Commit 4381186

Browse files
committed
Support KeepAlive to avoid duplicate connections
1 parent 5e0dcab commit 4381186

File tree

2 files changed

+31
-25
lines changed

2 files changed

+31
-25
lines changed

src/Transport/CurlerRequest.php

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class CurlerRequest
106106
/**
107107
* @param bool $id
108108
*/
109-
public function __construct($id = false)
109+
public function __construct($id = false, $handle = null)
110110
{
111111
$this->id = $id;
112112

@@ -128,23 +128,7 @@ public function __construct($id = false)
128128
CURLOPT_RETURNTRANSFER => TRUE,
129129
CURLOPT_USERAGENT => 'smi2/PHPClickHouse/client',
130130
);
131-
}
132-
133-
/**
134-
*
135-
*/
136-
public function __destruct()
137-
{
138-
$this->close();
139-
}
140-
141-
142-
public function close()
143-
{
144-
if ($this->handle) {
145-
curl_close($this->handle);
146-
}
147-
$this->handle = null;
131+
$this->handle = $handle;
148132
}
149133

150134
/**
@@ -375,10 +359,7 @@ public function isPersistent()
375359
*/
376360
public function keepAlive(int $sec = 60)
377361
{
378-
$this->options[CURLOPT_FORBID_REUSE] = TRUE;
379-
$this->headers['Connection'] = 'Keep-Alive';
380-
$this->headers['Keep-Alive'] = $sec;
381-
362+
$this->headers['Connection'] = 'keep-alive';
382363
return $this;
383364
}
384365

src/Transport/Http.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ class Http
9595
* @var null|resource
9696
*/
9797
private $stdErrOut = null;
98+
99+
/**
100+
* @var null|resource
101+
*/
102+
private $handle = null;
103+
98104
/**
99105
* Http constructor.
100106
* @param string $host
@@ -235,7 +241,7 @@ private function getUrl($params = []): string
235241
*/
236242
private function newRequest($extendinfo): CurlerRequest
237243
{
238-
$new = new CurlerRequest();
244+
$new = new CurlerRequest(false, $this->getHandle());
239245

240246
switch ($this->_authMethod) {
241247
case self::AUTH_METHOD_QUERY_STRING:
@@ -266,7 +272,8 @@ private function newRequest($extendinfo): CurlerRequest
266272
}
267273

268274
$new->timeOut($this->settings()->getTimeOut());
269-
$new->connectTimeOut($this->_connectTimeOut);//->keepAlive(); // one sec
275+
$new->connectTimeOut($this->_connectTimeOut);
276+
$new->keepAlive();
270277
$new->verbose(boolval($this->_verbose));
271278

272279
return $new;
@@ -569,7 +576,7 @@ public function getRequestWrite(Query $query): CurlerRequest
569576
*/
570577
public function ping(): bool
571578
{
572-
$request = new CurlerRequest();
579+
$request = new CurlerRequest(false, $this->getHandle());
573580
$request->url($this->getUri())->verbose(false)->GET()->connectTimeOut($this->getConnectTimeOut());
574581
$this->_curler->execOne($request);
575582

@@ -802,4 +809,22 @@ public function streamWrite(Stream $streamWrite, $sql, $bindings = []): Statemen
802809
$request = $this->writeStreamData($sql);
803810
return $this->streaming($streamWrite, $request);
804811
}
812+
813+
public function __destruct()
814+
{
815+
if ($this->handle) {
816+
curl_close($this->handle);
817+
}
818+
}
819+
820+
821+
public function getHandle()
822+
{
823+
if (!$this->handle) {
824+
$this->handle = curl_init();
825+
}
826+
827+
return $this->handle;
828+
}
829+
805830
}

0 commit comments

Comments
 (0)