fixed infinite loop in grpc client when the server close the connection

This commit is contained in:
York.GU 2019-09-10 11:10:28 +08:00
parent 9ff58a6ca8
commit 1314667ad5
3 changed files with 28 additions and 1 deletions

View File

@ -14,6 +14,8 @@ namespace Hyperf\GrpcClient;
use Google\Protobuf\Internal\Message;
use Hyperf\Grpc\Parser;
use Hyperf\Grpc\StatusCode;
use Hyperf\GrpcClient\Exception\GrpcClientException;
use Hyperf\Utils\ApplicationContext;
use Hyperf\Utils\ChannelPool;
use InvalidArgumentException;
@ -83,6 +85,10 @@ class BaseClient
$deserialize
) {
$streamId = $this->send($this->buildRequest($method, $argument));
if ($streamId === 0) {
// this client should not be used after this exception
throw new GrpcClientException('failed to send request to server', StatusCode::INTERNAL);
}
return Parser::parseResponse($this->recv($streamId), $deserialize);
}

View File

@ -0,0 +1,17 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/
namespace Hyperf\GrpcClient\Exception;
class GrpcClientException extends \RuntimeException
{
}

View File

@ -168,6 +168,10 @@ class GrpcClient
$shouldKill = true;
} else {
$shouldKill = ! $this->getHttpClient()->connect();
if ($shouldKill) {
// this call will set connected of http client to false
$this->getHttpClient()->close();
}
}
// Clear the receive channel map
if (! empty($this->recvChannelMap)) {
@ -325,7 +329,7 @@ class GrpcClient
}
} else {
// If no response, then close all the connection.
if (! $this->isConnected() && $this->closeRecv()) {
if ($this->closeRecv()) {
break;
}
}