Skip to content

Commit 3b5906f

Browse files
authored
Support ping method for grpc client. (#6373)
1 parent ec4a1d6 commit 3b5906f

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/GrpcClient.php

+13-4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use InvalidArgumentException;
2020
use RuntimeException;
2121
use Swoole\Coroutine\Http2\Client as SwooleHttp2Client;
22+
use Swoole\Http2\Response;
2223

2324
class GrpcClient
2425
{
@@ -136,7 +137,7 @@ public function closeRecv()
136137
// If this channel has pending pop, we should push 'false' to negate the pop.
137138
// Otherwise we should release it directly.
138139
while ($channel->stats()['consumer_num'] !== 0) {
139-
$channel->push(false);
140+
$channel->push(-1);
140141
}
141142
$this->channelPool->release($channel);
142143
}
@@ -234,12 +235,12 @@ public function recv(int $streamId, float $timeout = null)
234235
$channel = $this->recvChannelMap[$streamId] ?? null;
235236
if ($channel instanceof Channel) {
236237
$response = $channel->pop($timeout === null ? $this->timeout : $timeout);
237-
// Pop timeout
238-
if ($response === false && $channel->errCode === SWOOLE_CHANNEL_TIMEOUT) {
238+
if ($response === -1) {
239239
unset($this->recvChannelMap[$streamId]);
240+
return false;
240241
}
241242
// Unset recvChannelMap arfter recv
242-
if (! $response->pipeline) {
243+
if (($response === false && $channel->errCode === SWOOLE_CHANNEL_TIMEOUT) || ($response instanceof Response && ! $response->pipeline)) {
243244
unset($this->recvChannelMap[$streamId]);
244245
if (! $channel->isEmpty()) {
245246
$channel->pop();
@@ -258,6 +259,11 @@ public function getErrCode(): int
258259
return $this->httpClient ? $this->httpClient->errCode : 0;
259260
}
260261

262+
public function ping(): bool
263+
{
264+
return $this->getHttpClient()->ping();
265+
}
266+
261267
/**
262268
* @param bool|float $yield
263269
*/
@@ -316,6 +322,9 @@ private function runReceiveCoroutine()
316322
break;
317323
}
318324
} else {
325+
if ($this->ping()) {
326+
continue;
327+
}
319328
// If no response, then close all the connection.
320329
if ($this->closeRecv()) {
321330
break;

0 commit comments

Comments
 (0)