From d5e52459b63bc2a27f707dd9c388639102aba6ef Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 18 Sep 2024 08:54:56 -0700 Subject: [PATCH 1/6] WIP --- src/Transport/Grpc/GrpcClient.php | 112 ++++++++++++++++++++++++++++++ src/Transport/GrpcTransport.php | 28 ++++---- 2 files changed, 126 insertions(+), 14 deletions(-) create mode 100644 src/Transport/Grpc/GrpcClient.php diff --git a/src/Transport/Grpc/GrpcClient.php b/src/Transport/Grpc/GrpcClient.php new file mode 100644 index 000000000..902d3d1e5 --- /dev/null +++ b/src/Transport/Grpc/GrpcClient.php @@ -0,0 +1,112 @@ +_simpleRequest($method, $argument, $deserialize, $metadata, $options); + } + + public function bidiRequest($method, $deserialize, $metadata = [], $options = []) + { + return $this->_bidiRequest($method, $deserialize, $metadata, $options); + } + + public function serverStreamRequest($method, $argument, $deserialize, $metadata = [], $options = []) + { + return $this->_serverStreamRequest($method, $argument, $deserialize, $metadata, $options); + } + + public function clientStreamRequest($method, $deserialize, $metadata = [], $options = []) + { + return $this->_clientStreamRequest($method, $deserialize, $metadata, $options); + } +} diff --git a/src/Transport/GrpcTransport.php b/src/Transport/GrpcTransport.php index 9d1e9af0e..5e2fda178 100644 --- a/src/Transport/GrpcTransport.php +++ b/src/Transport/GrpcTransport.php @@ -40,6 +40,7 @@ use Google\ApiCore\GrpcSupportTrait; use Google\ApiCore\ServerStream; use Google\ApiCore\ServiceAddressTrait; +use Google\ApiCore\Transport\Grpc\GrpcClient; use Google\ApiCore\Transport\Grpc\ServerStreamingCallWrapper; use Google\ApiCore\Transport\Grpc\UnaryInterceptorInterface; use Google\ApiCore\ValidationException; @@ -54,12 +55,14 @@ /** * A gRPC based transport implementation. */ -class GrpcTransport extends BaseStub implements TransportInterface +class GrpcTransport implements TransportInterface { use ValidationTrait; use GrpcSupportTrait; use ServiceAddressTrait; + private GrpcClient $client; + /** * @param string $hostname * @param array $opts @@ -79,14 +82,7 @@ class GrpcTransport extends BaseStub implements TransportInterface */ public function __construct(string $hostname, array $opts, Channel $channel = null, array $interceptors = []) { - if ($interceptors) { - $channel = Interceptor::intercept( - $channel ?: new Channel($hostname, $opts), - $interceptors - ); - } - - parent::__construct($hostname, $opts, $channel); + $this->client = new GrpcClient($hostname, $opts, $channel, $interceptors); } /** @@ -162,7 +158,7 @@ public function startBidiStreamingCall(Call $call, array $options) $this->verifyUniverseDomain($options); return new BidiStream( - $this->_bidiRequest( + $this->client->bidiRequest( '/' . $call->getMethod(), [$call->getDecodeType(), 'decode'], isset($options['headers']) ? $options['headers'] : [], @@ -177,11 +173,10 @@ public function startBidiStreamingCall(Call $call, array $options) */ public function startClientStreamingCall(Call $call, array $options) { - $this->verifyUniverseDomain($options); return new ClientStream( - $this->_clientStreamRequest( + $this->client->clientStreamRequest( '/' . $call->getMethod(), [$call->getDecodeType(), 'decode'], isset($options['headers']) ? $options['headers'] : [], @@ -205,7 +200,7 @@ public function startServerStreamingCall(Call $call, array $options) } // This simultaenously creates and starts a \Grpc\ServerStreamingCall. - $stream = $this->_serverStreamRequest( + $stream = $this->client->serverStreamRequest( '/' . $call->getMethod(), $message, [$call->getDecodeType(), 'decode'], @@ -225,7 +220,7 @@ public function startUnaryCall(Call $call, array $options) { $this->verifyUniverseDomain($options); - $unaryCall = $this->_simpleRequest( + $unaryCall = $this->client->simpleRequest( '/' . $call->getMethod(), $call->getMessage(), [$call->getDecodeType(), 'decode'], @@ -254,6 +249,11 @@ function () use ($unaryCall, $options, &$promise) { return $promise; } + public function close() + { + $this->client->close(); + } + private function verifyUniverseDomain(array $options) { if (isset($options['credentialsWrapper'])) { From a53fc3dc4b1968a1db5d155eb119c6f556091f04 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 18 Sep 2024 12:40:57 -0700 Subject: [PATCH 2/6] fix tests --- src/Transport/GrpcTransport.php | 66 +++++++++++++++++-- .../Unit/Transport/GrpcTransportTest.php | 15 +++-- 2 files changed, 71 insertions(+), 10 deletions(-) diff --git a/src/Transport/GrpcTransport.php b/src/Transport/GrpcTransport.php index 5e2fda178..7099b17f4 100644 --- a/src/Transport/GrpcTransport.php +++ b/src/Transport/GrpcTransport.php @@ -158,7 +158,7 @@ public function startBidiStreamingCall(Call $call, array $options) $this->verifyUniverseDomain($options); return new BidiStream( - $this->client->bidiRequest( + $this->_bidiRequest( '/' . $call->getMethod(), [$call->getDecodeType(), 'decode'], isset($options['headers']) ? $options['headers'] : [], @@ -176,7 +176,7 @@ public function startClientStreamingCall(Call $call, array $options) $this->verifyUniverseDomain($options); return new ClientStream( - $this->client->clientStreamRequest( + $this->_clientStreamRequest( '/' . $call->getMethod(), [$call->getDecodeType(), 'decode'], isset($options['headers']) ? $options['headers'] : [], @@ -200,7 +200,7 @@ public function startServerStreamingCall(Call $call, array $options) } // This simultaenously creates and starts a \Grpc\ServerStreamingCall. - $stream = $this->client->serverStreamRequest( + $stream = $this->_serverStreamRequest( '/' . $call->getMethod(), $message, [$call->getDecodeType(), 'decode'], @@ -220,7 +220,7 @@ public function startUnaryCall(Call $call, array $options) { $this->verifyUniverseDomain($options); - $unaryCall = $this->client->simpleRequest( + $unaryCall = $this->_simpleRequest( '/' . $call->getMethod(), $call->getMessage(), [$call->getDecodeType(), 'decode'], @@ -283,4 +283,62 @@ private static function loadClientCertSource(callable $clientCertSource) { return call_user_func($clientCertSource); } + + /** FOR TESTING */ + + /** + * @param string $method + * @param array $arguments + * @param callable $deserialize + */ + protected function _simpleRequest( + $method, + $arguments, + $deserialize, + array $metadata = [], + array $options = [] + ) { + return $this->client->simpleRequest($method, $arguments, $deserialize, $metadata, $options); + } + + /** + * @param string $method + * @param callable $deserialize + */ + protected function _clientStreamRequest( + $method, + $deserialize, + array $metadata = [], + array $options = [] + ) { + return $this->client->clientStreamRequest($method, $deserialize, $metadata, $options); + } + + /** + * @param string $method + * @param array $arguments + * @param callable $deserialize + */ + protected function _serverStreamRequest( + $method, + $arguments, + $deserialize, + array $metadata = [], + array $options = [] + ) { + return $this->client->serverStreamRequest($method, $arguments, $deserialize, $metadata, $options); + } + + /** + * @param string $method + * @param callable $deserialize + */ + protected function _bidiRequest( + $method, + $deserialize, + array $metadata = [], + array $options = [] + ) { + return $this->client->bidiRequest($method, $deserialize, $metadata, $options); + } } diff --git a/tests/Tests/Unit/Transport/GrpcTransportTest.php b/tests/Tests/Unit/Transport/GrpcTransportTest.php index 9d8e37d70..d8704a685 100644 --- a/tests/Tests/Unit/Transport/GrpcTransportTest.php +++ b/tests/Tests/Unit/Transport/GrpcTransportTest.php @@ -505,21 +505,24 @@ public function buildInvalidData() */ public function testExperimentalInterceptors($callType, $interceptor) { + $mockCallInvoker = new MockCallInvoker($this->buildMockCallForInterceptor($callType)); + $transport = new GrpcTransport( 'example.com', [ - 'credentials' => ChannelCredentials::createInsecure() + 'credentials' => ChannelCredentials::createInsecure(), ], null, [$interceptor] ); - $mockCallInvoker = new MockCallInvoker($this->buildMockCallForInterceptor($callType)); + $r1 = new \ReflectionProperty(GrpcTransport::class, 'client'); + $r1->setAccessible(true); - $r = new \ReflectionProperty(BaseStub::class, 'call_invoker'); - $r->setAccessible(true); - $r->setValue( - $transport, + $r2 = new \ReflectionProperty(BaseStub::class, 'call_invoker'); + $r2->setAccessible(true); + $r2->setValue( + $r1->getValue($transport), $mockCallInvoker ); From 5fe655f1b3d02043ff338ec33fcd1137c91307f8 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 18 Sep 2024 12:45:30 -0700 Subject: [PATCH 3/6] remove GrpcClient --- src/Transport/Grpc/GrpcClient.php | 112 ------------------------------ src/Transport/GrpcTransport.php | 23 +++--- 2 files changed, 15 insertions(+), 120 deletions(-) delete mode 100644 src/Transport/Grpc/GrpcClient.php diff --git a/src/Transport/Grpc/GrpcClient.php b/src/Transport/Grpc/GrpcClient.php deleted file mode 100644 index 902d3d1e5..000000000 --- a/src/Transport/Grpc/GrpcClient.php +++ /dev/null @@ -1,112 +0,0 @@ -_simpleRequest($method, $argument, $deserialize, $metadata, $options); - } - - public function bidiRequest($method, $deserialize, $metadata = [], $options = []) - { - return $this->_bidiRequest($method, $deserialize, $metadata, $options); - } - - public function serverStreamRequest($method, $argument, $deserialize, $metadata = [], $options = []) - { - return $this->_serverStreamRequest($method, $argument, $deserialize, $metadata, $options); - } - - public function clientStreamRequest($method, $deserialize, $metadata = [], $options = []) - { - return $this->_clientStreamRequest($method, $deserialize, $metadata, $options); - } -} diff --git a/src/Transport/GrpcTransport.php b/src/Transport/GrpcTransport.php index 7099b17f4..41130e8f7 100644 --- a/src/Transport/GrpcTransport.php +++ b/src/Transport/GrpcTransport.php @@ -46,22 +46,22 @@ use Google\ApiCore\ValidationException; use Google\ApiCore\ValidationTrait; use Google\Rpc\Code; -use Grpc\BaseStub; use Grpc\Channel; use Grpc\ChannelCredentials; use Grpc\Interceptor; +use Grpc\BaseStub; use GuzzleHttp\Promise\Promise; /** * A gRPC based transport implementation. */ -class GrpcTransport implements TransportInterface +class GrpcTransport extends BaseStub implements TransportInterface { use ValidationTrait; use GrpcSupportTrait; use ServiceAddressTrait; - private GrpcClient $client; + private BaseStub $client; /** * @param string $hostname @@ -82,7 +82,14 @@ class GrpcTransport implements TransportInterface */ public function __construct(string $hostname, array $opts, Channel $channel = null, array $interceptors = []) { - $this->client = new GrpcClient($hostname, $opts, $channel, $interceptors); + if ($interceptors) { + $channel = Interceptor::intercept( + $channel ?: new Channel($hostname, $opts), + $interceptors + ); + } + + $this->client = new BaseStub($hostname, $opts, $channel, $interceptors); } /** @@ -298,7 +305,7 @@ protected function _simpleRequest( array $metadata = [], array $options = [] ) { - return $this->client->simpleRequest($method, $arguments, $deserialize, $metadata, $options); + return $this->client->_simpleRequest($method, $arguments, $deserialize, $metadata, $options); } /** @@ -311,7 +318,7 @@ protected function _clientStreamRequest( array $metadata = [], array $options = [] ) { - return $this->client->clientStreamRequest($method, $deserialize, $metadata, $options); + return $this->client->_clientStreamRequest($method, $deserialize, $metadata, $options); } /** @@ -326,7 +333,7 @@ protected function _serverStreamRequest( array $metadata = [], array $options = [] ) { - return $this->client->serverStreamRequest($method, $arguments, $deserialize, $metadata, $options); + return $this->client->_serverStreamRequest($method, $arguments, $deserialize, $metadata, $options); } /** @@ -339,6 +346,6 @@ protected function _bidiRequest( array $metadata = [], array $options = [] ) { - return $this->client->bidiRequest($method, $deserialize, $metadata, $options); + return $this->client->_bidiRequest($method, $deserialize, $metadata, $options); } } From 8753cb9c942cced115dbea14bc3c9063d7eb975d Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 18 Sep 2024 12:46:33 -0700 Subject: [PATCH 4/6] rename property --- src/Transport/GrpcTransport.php | 16 ++++++++-------- tests/Tests/Unit/Transport/GrpcTransportTest.php | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Transport/GrpcTransport.php b/src/Transport/GrpcTransport.php index 41130e8f7..66d9952b8 100644 --- a/src/Transport/GrpcTransport.php +++ b/src/Transport/GrpcTransport.php @@ -46,10 +46,10 @@ use Google\ApiCore\ValidationException; use Google\ApiCore\ValidationTrait; use Google\Rpc\Code; +use Grpc\BaseStub; use Grpc\Channel; use Grpc\ChannelCredentials; use Grpc\Interceptor; -use Grpc\BaseStub; use GuzzleHttp\Promise\Promise; /** @@ -61,7 +61,7 @@ class GrpcTransport extends BaseStub implements TransportInterface use GrpcSupportTrait; use ServiceAddressTrait; - private BaseStub $client; + private BaseStub $stub; /** * @param string $hostname @@ -89,7 +89,7 @@ public function __construct(string $hostname, array $opts, Channel $channel = nu ); } - $this->client = new BaseStub($hostname, $opts, $channel, $interceptors); + $this->stub = new BaseStub($hostname, $opts, $channel, $interceptors); } /** @@ -258,7 +258,7 @@ function () use ($unaryCall, $options, &$promise) { public function close() { - $this->client->close(); + $this->stub->close(); } private function verifyUniverseDomain(array $options) @@ -305,7 +305,7 @@ protected function _simpleRequest( array $metadata = [], array $options = [] ) { - return $this->client->_simpleRequest($method, $arguments, $deserialize, $metadata, $options); + return $this->stub->_simpleRequest($method, $arguments, $deserialize, $metadata, $options); } /** @@ -318,7 +318,7 @@ protected function _clientStreamRequest( array $metadata = [], array $options = [] ) { - return $this->client->_clientStreamRequest($method, $deserialize, $metadata, $options); + return $this->stub->_clientStreamRequest($method, $deserialize, $metadata, $options); } /** @@ -333,7 +333,7 @@ protected function _serverStreamRequest( array $metadata = [], array $options = [] ) { - return $this->client->_serverStreamRequest($method, $arguments, $deserialize, $metadata, $options); + return $this->stub->_serverStreamRequest($method, $arguments, $deserialize, $metadata, $options); } /** @@ -346,6 +346,6 @@ protected function _bidiRequest( array $metadata = [], array $options = [] ) { - return $this->client->_bidiRequest($method, $deserialize, $metadata, $options); + return $this->stub->_bidiRequest($method, $deserialize, $metadata, $options); } } diff --git a/tests/Tests/Unit/Transport/GrpcTransportTest.php b/tests/Tests/Unit/Transport/GrpcTransportTest.php index d8704a685..6ab705746 100644 --- a/tests/Tests/Unit/Transport/GrpcTransportTest.php +++ b/tests/Tests/Unit/Transport/GrpcTransportTest.php @@ -516,7 +516,7 @@ public function testExperimentalInterceptors($callType, $interceptor) [$interceptor] ); - $r1 = new \ReflectionProperty(GrpcTransport::class, 'client'); + $r1 = new \ReflectionProperty(GrpcTransport::class, 'stub'); $r1->setAccessible(true); $r2 = new \ReflectionProperty(BaseStub::class, 'call_invoker'); From 206bff924fa932b84a86ffcb592f6d552b631e65 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Thu, 19 Sep 2024 06:48:20 -0700 Subject: [PATCH 5/6] fix phpstan --- src/Transport/GrpcTransport.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Transport/GrpcTransport.php b/src/Transport/GrpcTransport.php index 66d9952b8..27d3f8530 100644 --- a/src/Transport/GrpcTransport.php +++ b/src/Transport/GrpcTransport.php @@ -89,7 +89,7 @@ public function __construct(string $hostname, array $opts, Channel $channel = nu ); } - $this->stub = new BaseStub($hostname, $opts, $channel, $interceptors); + $this->stub = new BaseStub($hostname, $opts, $channel); } /** From 9d69096f08a41efcaa6be16f7c740f8a0abd905d Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Thu, 19 Sep 2024 06:50:53 -0700 Subject: [PATCH 6/6] fix cs --- src/GapicClientTrait.php | 2 +- src/Testing/MockClientStreamingCall.php | 2 +- src/Transport/GrpcFallbackTransport.php | 2 +- src/Transport/GrpcTransport.php | 1 - src/Transport/Rest/JsonStreamDecoder.php | 4 ++-- 5 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/GapicClientTrait.php b/src/GapicClientTrait.php index 195b06f6d..ebacb3bc7 100644 --- a/src/GapicClientTrait.php +++ b/src/GapicClientTrait.php @@ -515,7 +515,7 @@ private function startApiCall( Message $request = null, array $optionalArgs = [] ) { - $methodDescriptors =$this->validateCallConfig($methodName); + $methodDescriptors = $this->validateCallConfig($methodName); $callType = $methodDescriptors['callType']; // Prepare request-based headers, merge with user-provided headers, diff --git a/src/Testing/MockClientStreamingCall.php b/src/Testing/MockClientStreamingCall.php index c7cb8696f..439a547e9 100644 --- a/src/Testing/MockClientStreamingCall.php +++ b/src/Testing/MockClientStreamingCall.php @@ -86,7 +86,7 @@ public function wait() public function write($request, array $options = []) { if ($this->waitCalled) { - throw new ApiException('Cannot call write() after wait()', Code::INTERNAL, ApiStatus::INTERNAL); + throw new ApiException('Cannot call write() after wait()', Code::INTERNAL, ApiStatus::INTERNAL); } if (is_a($request, '\Google\Protobuf\Internal\Message')) { /** @var Message $newRequest */ diff --git a/src/Transport/GrpcFallbackTransport.php b/src/Transport/GrpcFallbackTransport.php index c20f3c0b1..808c176c7 100644 --- a/src/Transport/GrpcFallbackTransport.php +++ b/src/Transport/GrpcFallbackTransport.php @@ -160,7 +160,7 @@ private function unpackResponse(Call $call, ResponseInterface $response) $decodeType = $call->getDecodeType(); /** @var Message $responseMessage */ $responseMessage = new $decodeType(); - $responseMessage->mergeFromString((string)$response->getBody()); + $responseMessage->mergeFromString((string) $response->getBody()); return $responseMessage; } diff --git a/src/Transport/GrpcTransport.php b/src/Transport/GrpcTransport.php index 27d3f8530..1d8869aab 100644 --- a/src/Transport/GrpcTransport.php +++ b/src/Transport/GrpcTransport.php @@ -40,7 +40,6 @@ use Google\ApiCore\GrpcSupportTrait; use Google\ApiCore\ServerStream; use Google\ApiCore\ServiceAddressTrait; -use Google\ApiCore\Transport\Grpc\GrpcClient; use Google\ApiCore\Transport\Grpc\ServerStreamingCallWrapper; use Google\ApiCore\Transport\Grpc\UnaryInterceptorInterface; use Google\ApiCore\ValidationException; diff --git a/src/Transport/Rest/JsonStreamDecoder.php b/src/Transport/Rest/JsonStreamDecoder.php index b173cf3a1..c01f21135 100644 --- a/src/Transport/Rest/JsonStreamDecoder.php +++ b/src/Transport/Rest/JsonStreamDecoder.php @@ -164,7 +164,7 @@ private function _decode() if ($b === '}' && !$str) { $level--; if ($level === 1) { - $end = $cursor+1; + $end = $cursor + 1; } } // Track the closing of an array if not in a string value. @@ -195,7 +195,7 @@ private function _decode() // Dump the part of the chunk used for parsing the message // and use the remaining for the next message. - $remaining = $chunkLength-$length; + $remaining = $chunkLength - $length; $chunk = substr($chunk, $end, $remaining); // Reset all indices and exit chunk processing.