Skip to content

Commit 9a253ef

Browse files
committed
优化 Http2Client
1 parent e0f50f4 commit 9a253ef

1 file changed

Lines changed: 30 additions & 19 deletions

File tree

src/YurunHttp/Http2/SwooleClient.php

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -144,15 +144,17 @@ public function getHttpHandler()
144144
*/
145145
public function close()
146146
{
147-
$this->http2Client = null;
148-
$url = ($this->ssl ? 'https://' : 'http://') . $this->host . ':' . $this->port;
149-
$this->handler->getHttp2ConnectionManager()->closeConnection($url);
147+
$this->recvCo = false;
150148
$recvChannels = &$this->recvChannels;
151149
foreach ($recvChannels as $channel)
152150
{
153151
$channel->close();
154152
}
155153
$recvChannels = [];
154+
$this->requestMap = [];
155+
$this->http2Client = null;
156+
$url = ($this->ssl ? 'https://' : 'http://') . $this->host . ':' . $this->port;
157+
$this->handler->getHttp2ConnectionManager()->closeConnection($url);
156158
}
157159

158160
/**
@@ -180,14 +182,17 @@ public function send($request, $pipeline = false, $dropRecvResponse = false)
180182
$request = $request->withAttribute(Attributes::HTTP2_PIPELINE, $pipeline);
181183
$this->handler->buildRequest($request, $http2Client, $http2Request);
182184
$streamId = $http2Client->send($http2Request);
183-
if (!$streamId)
185+
if ($streamId)
184186
{
185-
$this->close();
187+
if (!$dropRecvResponse)
188+
{
189+
$this->recvChannels[$streamId] = new Channel(1);
190+
$this->requestMap[$streamId] = $request;
191+
}
186192
}
187-
if (!$dropRecvResponse)
193+
else
188194
{
189-
$this->recvChannels[$streamId] = new Channel(1);
190-
$this->requestMap[$streamId] = $request;
195+
$this->close();
191196
}
192197

193198
return $streamId;
@@ -230,7 +235,7 @@ public function end($streamId)
230235
public function recv($streamId = -1, $timeout = null)
231236
{
232237
$recvCo = $this->recvCo;
233-
if (!$recvCo || (true !== $recvCo && !Coroutine::exists($recvCo)))
238+
if (!$recvCo || !Coroutine::exists($recvCo))
234239
{
235240
$this->startRecvCo();
236241
}
@@ -243,12 +248,6 @@ public function recv($streamId = -1, $timeout = null)
243248
{
244249
$recvChannels[$streamId] = $channel = new Channel(-1 === $streamId ? $this->serverPushQueueLength : 1);
245250
}
246-
$swooleResponse = $channel->pop(null === $timeout ? -1 : $timeout);
247-
if (-1 !== $streamId)
248-
{
249-
unset($recvChannels[$streamId]);
250-
$channel->close();
251-
}
252251
$requestMap = &$this->requestMap;
253252
if (isset($requestMap[$streamId]))
254253
{
@@ -259,6 +258,15 @@ public function recv($streamId = -1, $timeout = null)
259258
{
260259
$request = null;
261260
}
261+
$swooleResponse = $channel->pop(null === $timeout ? -1 : $timeout);
262+
if ($this->recvCo === $recvCo)
263+
{
264+
if (-1 !== $streamId)
265+
{
266+
unset($recvChannels[$streamId]);
267+
$channel->close();
268+
}
269+
}
262270
$response = $this->handler->buildHttp2Response($request, $this->http2Client, $swooleResponse);
263271

264272
return $response;
@@ -286,13 +294,12 @@ private function startRecvCo()
286294
{
287295
return false;
288296
}
289-
$recvCo = &$this->recvCo;
290-
$recvCo = true;
291297

292-
return $recvCo = Coroutine::create(function () {
298+
return Coroutine::create(function () {
299+
$this->recvCo = $coid = Coroutine::getCid();
293300
$http2Client = &$this->http2Client;
294301
$recvChannels = &$this->recvChannels;
295-
while ($this->isConnected())
302+
while ($coid === $this->recvCo && $this->isConnected())
296303
{
297304
if ($this->timeout > 0)
298305
{
@@ -302,6 +309,10 @@ private function startRecvCo()
302309
{
303310
$swooleResponse = $http2Client->recv();
304311
}
312+
if ($coid !== $this->recvCo)
313+
{
314+
return;
315+
}
305316
if (!$swooleResponse)
306317
{
307318
if ($this->ping())

0 commit comments

Comments
 (0)