mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-12-02 03:37:44 +08:00
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:
commit
7e34e9075d
@ -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
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
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) {
|
||||
// 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