19
19
use InvalidArgumentException ;
20
20
use RuntimeException ;
21
21
use Swoole \Coroutine \Http2 \Client as SwooleHttp2Client ;
22
+ use Swoole \Http2 \Response ;
22
23
23
24
class GrpcClient
24
25
{
@@ -136,7 +137,7 @@ public function closeRecv()
136
137
// If this channel has pending pop, we should push 'false' to negate the pop.
137
138
// Otherwise we should release it directly.
138
139
while ($ channel ->stats ()['consumer_num ' ] !== 0 ) {
139
- $ channel ->push (false );
140
+ $ channel ->push (- 1 );
140
141
}
141
142
$ this ->channelPool ->release ($ channel );
142
143
}
@@ -234,12 +235,12 @@ public function recv(int $streamId, float $timeout = null)
234
235
$ channel = $ this ->recvChannelMap [$ streamId ] ?? null ;
235
236
if ($ channel instanceof Channel) {
236
237
$ response = $ channel ->pop ($ timeout === null ? $ this ->timeout : $ timeout );
237
- // Pop timeout
238
- if ($ response === false && $ channel ->errCode === SWOOLE_CHANNEL_TIMEOUT ) {
238
+ if ($ response === -1 ) {
239
239
unset($ this ->recvChannelMap [$ streamId ]);
240
+ return false ;
240
241
}
241
242
// Unset recvChannelMap arfter recv
242
- if (! $ response ->pipeline ) {
243
+ if (( $ response === false && $ channel -> errCode === SWOOLE_CHANNEL_TIMEOUT ) || ( $ response instanceof Response && ! $ response ->pipeline ) ) {
243
244
unset($ this ->recvChannelMap [$ streamId ]);
244
245
if (! $ channel ->isEmpty ()) {
245
246
$ channel ->pop ();
@@ -258,6 +259,11 @@ public function getErrCode(): int
258
259
return $ this ->httpClient ? $ this ->httpClient ->errCode : 0 ;
259
260
}
260
261
262
+ public function ping (): bool
263
+ {
264
+ return $ this ->getHttpClient ()->ping ();
265
+ }
266
+
261
267
/**
262
268
* @param bool|float $yield
263
269
*/
@@ -316,6 +322,9 @@ private function runReceiveCoroutine()
316
322
break ;
317
323
}
318
324
} else {
325
+ if ($ this ->ping ()) {
326
+ continue ;
327
+ }
319
328
// If no response, then close all the connection.
320
329
if ($ this ->closeRecv ()) {
321
330
break ;
0 commit comments