Merge pull request #551 from starfalling/1.0

Fixed infinite loop in grpc client when the server closed the connection
This commit is contained in:
李铭昕 2019-09-10 11:47:04 +08:00 committed by GitHub
commit 7e34e9075d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 1 deletions

View File

@ -4,6 +4,7 @@
- [#534](https://github.com/hyperf-cloud/hyperf/pull/534) Fixed Guzzle HTTP Client does not handle the response status is equal to `-3`;
- [#541](https://github.com/hyperf-cloud/hyperf/pull/541) Fixed bug grpc client cannot be set correctly.
- [#551](https://github.com/hyperf-cloud/hyperf/pull/551) Fixed infinite loop in grpc client when the server closed the connection.
## Deleted

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;
@ -75,6 +77,7 @@ class BaseClient
* @param array $metadata A metadata map to send to the server
* (optional)
* @param array $options An array of options (optional)
* @throws GrpcClientException The client should not be used after this exception
* @return []
*/
protected function simpleRequest(
@ -83,6 +86,10 @@ class BaseClient
$deserialize
) {
$streamId = $this->send($this->buildRequest($method, $argument));
if ($streamId === 0) {
// The client should not be used after this exception
throw new GrpcClientException('Failed to send the 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) {
// 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;
}
}