From 1df8f305850387d82da1e22a133600cad7000541 Mon Sep 17 00:00:00 2001 From: Reasno Date: Mon, 14 Sep 2020 13:24:37 +0800 Subject: [PATCH] fix: throw exceptions when disconnect --- src/grpc-client/src/Request.php | 2 +- src/grpc-client/src/ServerStreamingCall.php | 2 +- src/grpc-client/src/StreamingCall.php | 33 +++++++++++++------ src/grpc-client/tests/Routeguide/Feature.php | 2 +- .../tests/Routeguide/Rectangle.php | 2 +- .../tests/Routeguide/RouteNote.php | 2 +- .../tests/Routeguide/RouteSummary.php | 6 ++-- 7 files changed, 31 insertions(+), 18 deletions(-) diff --git a/src/grpc-client/src/Request.php b/src/grpc-client/src/Request.php index 3c904c508..88f758c66 100644 --- a/src/grpc-client/src/Request.php +++ b/src/grpc-client/src/Request.php @@ -21,7 +21,7 @@ class Request extends BaseRequest private const DEFAULT_CONTENT_TYPE = 'application/grpc+proto'; /** - * @var bool $usePipelineRead + * @var bool */ public $usePipelineRead; diff --git a/src/grpc-client/src/ServerStreamingCall.php b/src/grpc-client/src/ServerStreamingCall.php index 838591f66..e5090ffe0 100644 --- a/src/grpc-client/src/ServerStreamingCall.php +++ b/src/grpc-client/src/ServerStreamingCall.php @@ -19,7 +19,7 @@ use Hyperf\GrpcClient\Exception\GrpcClientException; */ class ServerStreamingCall extends StreamingCall { - public function push($message): bool + public function push($message): void { throw new GrpcClientException('ServerStreamingCall can not push data by client'); } diff --git a/src/grpc-client/src/StreamingCall.php b/src/grpc-client/src/StreamingCall.php index 33cd81146..b78a54c40 100644 --- a/src/grpc-client/src/StreamingCall.php +++ b/src/grpc-client/src/StreamingCall.php @@ -12,6 +12,7 @@ declare(strict_types=1); namespace Hyperf\GrpcClient; use Hyperf\Grpc\Parser; +use Hyperf\GrpcClient\Exception\GrpcClientException; use RuntimeException; class StreamingCall @@ -65,7 +66,7 @@ class StreamingCall return $this; } - public function send($message = null): bool + public function send($message = null): void { if (! $this->getStreamId()) { $this->setStreamId($this->client->openStream( @@ -74,17 +75,22 @@ class StreamingCall '', true )); - return $this->getStreamId() > 0; + if ($this->getStreamId() <= 0) { + throw $this->newException(); + } } throw new RuntimeException('You can only send once by a streaming call except connection closed and you retry.'); } - public function push($message): bool + public function push($message): void { if (! $this->getStreamId()) { $this->setStreamId($this->client->openStream($this->method, null, '', true)); } - return $this->client->write($this->getStreamId(), Parser::serializeMessage($message), false); + $success = $this->client->write($this->getStreamId(), Parser::serializeMessage($message), false); + if (! $success) { + throw $this->newException(); + } } public function recv(float $timeout = -1.0) @@ -98,10 +104,9 @@ class StreamingCall $this->streamId = 0; } } - // disconnected + // disconnected or timed out if ($recv === false) { - $this->streamId = 0; - return[null, 14, $recv]; + throw $this->newException(); } // server ended the stream if ($recv->pipeline === false) { @@ -112,12 +117,20 @@ class StreamingCall return Parser::parseResponse($recv, $this->deserialize); } - public function end(): bool + public function end(): void { if (! $this->getStreamId()) { - return false; + throw $this->newException(); } // we cannot reset the streamId here, otherwise the client streaming will break. - return $this->client->write($this->getStreamId(), null, true); + $success = $this->client->write($this->getStreamId(), null, true); + if (! $success) { + throw $this->newException(); + } + } + + private function newException(): GrpcClientException + { + return new GrpcClientException('the remote server may have been disconnected or timed out'); } } diff --git a/src/grpc-client/tests/Routeguide/Feature.php b/src/grpc-client/tests/Routeguide/Feature.php index 58a7a0f1a..488349e37 100644 --- a/src/grpc-client/tests/Routeguide/Feature.php +++ b/src/grpc-client/tests/Routeguide/Feature.php @@ -44,7 +44,7 @@ class Feature extends \Google\Protobuf\Internal\Message * Optional. Data for populating the Message object. * * @var string $name - * The name of the feature. + * The name of the feature * @var \Routeguide\Point $location * The point where the feature is detected. * } diff --git a/src/grpc-client/tests/Routeguide/Rectangle.php b/src/grpc-client/tests/Routeguide/Rectangle.php index d5130f082..a29fd377a 100644 --- a/src/grpc-client/tests/Routeguide/Rectangle.php +++ b/src/grpc-client/tests/Routeguide/Rectangle.php @@ -44,7 +44,7 @@ class Rectangle extends \Google\Protobuf\Internal\Message * Optional. Data for populating the Message object. * * @var \Routeguide\Point $lo - * One corner of the rectangle. + * One corner of the rectangle * @var \Routeguide\Point $hi * The other corner of the rectangle. * } diff --git a/src/grpc-client/tests/Routeguide/RouteNote.php b/src/grpc-client/tests/Routeguide/RouteNote.php index 08f3e0df3..a48748ae4 100644 --- a/src/grpc-client/tests/Routeguide/RouteNote.php +++ b/src/grpc-client/tests/Routeguide/RouteNote.php @@ -43,7 +43,7 @@ class RouteNote extends \Google\Protobuf\Internal\Message * Optional. Data for populating the Message object. * * @var \Routeguide\Point $location - * The location from which the message is sent. + * The location from which the message is sent * @var string $message * The message to be sent. * } diff --git a/src/grpc-client/tests/Routeguide/RouteSummary.php b/src/grpc-client/tests/Routeguide/RouteSummary.php index a2c537b18..c6e1b13e4 100644 --- a/src/grpc-client/tests/Routeguide/RouteSummary.php +++ b/src/grpc-client/tests/Routeguide/RouteSummary.php @@ -60,11 +60,11 @@ class RouteSummary extends \Google\Protobuf\Internal\Message * Optional. Data for populating the Message object. * * @var int $point_count - * The number of points received. + * The number of points received * @var int $feature_count - * The number of known features passed while traversing the route. + * The number of known features passed while traversing the route * @var int $distance - * The distance covered in metres. + * The distance covered in metres * @var int $elapsed_time * The duration of the traversal in seconds. * }