generated
3 years ago
ChannelRef.php
3 years ago
Config.php
3 years ago
CreatedByDeserializeCheck.php
3 years ago
GCPBidiStreamingCall.php
3 years ago
GCPCallInvoker.php
3 years ago
GCPClientStreamCall.php
3 years ago
GCPServerStreamCall.php
3 years ago
GCPUnaryCall.php
3 years ago
GcpBaseCall.php
3 years ago
GcpExtensionChannel.php
3 years ago
grpc_gcp.proto
3 years ago
ChannelRef.php
99 lines
| 1 | <?php |
| 2 | /* |
| 3 | * |
| 4 | * Copyright 2018 gRPC authors. |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * |
| 18 | */ |
| 19 | namespace Grpc\Gcp; |
| 20 | |
| 21 | /** |
| 22 | * ChannelRef is used to record how many active streams the channel has. |
| 23 | * This is a private class |
| 24 | */ |
| 25 | class ChannelRef |
| 26 | { |
| 27 | // $opts has all information except Credentials for creating a Grpc\Channel. |
| 28 | private $opts; |
| 29 | |
| 30 | private $channel_id; |
| 31 | private $affinity_ref; |
| 32 | private $active_stream_ref; |
| 33 | private $target; |
| 34 | |
| 35 | private $has_deserialized; |
| 36 | private $real_channel; |
| 37 | |
| 38 | public function __construct($target, $channel_id, $opts, $affinity_ref=0, $active_stream_ref=0) |
| 39 | { |
| 40 | $this->target = $target; |
| 41 | $this->channel_id = $channel_id; |
| 42 | $this->affinity_ref = $affinity_ref; |
| 43 | $this->active_stream_ref = $active_stream_ref; |
| 44 | $this->opts = $opts; |
| 45 | $this->has_deserialized = new CreatedByDeserializeCheck(); |
| 46 | } |
| 47 | |
| 48 | public function getRealChannel($credentials) |
| 49 | { |
| 50 | // TODO(ddyihai): remove this check once the serialize handler for |
| 51 | // \Grpc\Channel is implemented(issue https://github.com/grpc/grpc/issues/15870). |
| 52 | if (!$this->has_deserialized->getData()) { |
| 53 | // $real_channel exists and is not created by the deserialization. |
| 54 | return $this->real_channel; |
| 55 | } |
| 56 | // If this ChannelRef is created by deserialization, $real_channel is invalid |
| 57 | // thus needs to be recreated becasue Grpc\Channel don't have serialize and |
| 58 | // deserialize handler. |
| 59 | // Since [target + augments + credentials] will be the same during the recreation, |
| 60 | // it will reuse the underline grpc channel in C extension without creating a |
| 61 | // new connection. |
| 62 | |
| 63 | // 'credentials' in the array $opts will be unset during creating the channel. |
| 64 | if (!array_key_exists('credentials', $this->opts)) { |
| 65 | $this->opts['credentials'] = $credentials; |
| 66 | } |
| 67 | $real_channel = new \Grpc\Channel($this->target, $this->opts); |
| 68 | $this->real_channel = $real_channel; |
| 69 | // Set deserialization to false so it won't be recreated within the same script. |
| 70 | $this->has_deserialized->setData(0); |
| 71 | return $real_channel; |
| 72 | } |
| 73 | |
| 74 | public function getAffinityRef() |
| 75 | { |
| 76 | return $this->affinity_ref; |
| 77 | } |
| 78 | public function getActiveStreamRef() |
| 79 | { |
| 80 | return $this->active_stream_ref; |
| 81 | } |
| 82 | public function affinityRefIncr() |
| 83 | { |
| 84 | $this->affinity_ref += 1; |
| 85 | } |
| 86 | public function affinityRefDecr() |
| 87 | { |
| 88 | $this->affinity_ref -= 1; |
| 89 | } |
| 90 | public function activeStreamRefIncr() |
| 91 | { |
| 92 | $this->active_stream_ref += 1; |
| 93 | } |
| 94 | public function activeStreamRefDecr() |
| 95 | { |
| 96 | $this->active_stream_ref -= 1; |
| 97 | } |
| 98 | } |
| 99 |