mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-11-30 10:47:44 +08:00
fixed infinite loop in grpc client when the server close the connection
This commit is contained in:
parent
9ff58a6ca8
commit
1314667ad5
@ -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);
|
||||
}
|
||||
|
||||
|
17
src/grpc-client/src/Exception/GrpcClientException.php
Normal file
17
src/grpc-client/src/Exception/GrpcClientException.php
Normal 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
|
||||
{
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user