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
GcpBaseCall.php
223 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 | abstract class GcpBaseCall |
| 22 | { |
| 23 | const BOUND = 'BOUND'; |
| 24 | const UNBIND = 'UNBIND'; |
| 25 | const BIND = 'BIND'; |
| 26 | |
| 27 | |
| 28 | protected $gcp_channel; |
| 29 | // It has the Grpc\Channel and related ref_count information for this RPC. |
| 30 | protected $channel_ref; |
| 31 | // If this RPC is 'UNBIND', use it instead of the one from response. |
| 32 | protected $affinity_key; |
| 33 | // Array of [affinity_key, command] |
| 34 | protected $_affinity; |
| 35 | |
| 36 | // Information needed to create Grpc\Call object when the RPC starts. |
| 37 | protected $method; |
| 38 | protected $argument; |
| 39 | protected $metadata; |
| 40 | protected $options; |
| 41 | |
| 42 | // In GCP extension, it is when a RPC calls "start", we pick a channel. |
| 43 | // Thus we need to save the $me |
| 44 | protected $metadata_rpc = array(); |
| 45 | // first_rpc is used to check whether the first request is sent for client |
| 46 | // streaming RPC. |
| 47 | protected $has_real_call = null; |
| 48 | |
| 49 | protected $real_call; |
| 50 | |
| 51 | /** |
| 52 | * Create a new Call wrapper object. |
| 53 | * |
| 54 | * @param Channel $channel The channel to communicate on |
| 55 | * @param string $method The method to call on the |
| 56 | * remote server |
| 57 | * @param callback $deserialize A callback function to deserialize |
| 58 | * the response |
| 59 | * @param array $options Call options (optional) |
| 60 | */ |
| 61 | public function __construct($channel, $method, $deserialize, $options) |
| 62 | { |
| 63 | $this->gcp_channel = $channel; |
| 64 | $this->method = $method; |
| 65 | $this->deserialize = $deserialize; |
| 66 | $this->options = $options; |
| 67 | $this->_affinity = null; |
| 68 | |
| 69 | if (isset($this->gcp_channel->affinity_conf['affinity_by_method'][$method])) { |
| 70 | $this->_affinity = $this->gcp_channel->affinity_conf['affinity_by_method'][$method]; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Pick a ChannelRef from the channel pool based on the request and |
| 76 | * the affinity config. |
| 77 | * |
| 78 | * @param mixed $argument Requests. |
| 79 | * |
| 80 | * @return ChannelRef |
| 81 | */ |
| 82 | protected function _rpcPreProcess($argument) |
| 83 | { |
| 84 | $this->affinity_key = null; |
| 85 | if ($this->_affinity) { |
| 86 | $command = $this->_affinity['command']; |
| 87 | if ($command == self::BOUND || $command == self::UNBIND) { |
| 88 | $this->affinity_key = $this->getAffinityKeyFromProto($argument); |
| 89 | } |
| 90 | } |
| 91 | $this->channel_ref = $this->gcp_channel->getChannelRef($this->affinity_key); |
| 92 | $this->channel_ref->activeStreamRefIncr(); |
| 93 | return $this->channel_ref; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Update ChannelRef when RPC finishes. |
| 98 | * |
| 99 | * @param \stdClass $status The status object, with integer $code, string |
| 100 | * $details, and array $metadata members |
| 101 | * @param mixed $response Response. |
| 102 | */ |
| 103 | protected function _rpcPostProcess($status, $response) |
| 104 | { |
| 105 | if ($this->_affinity) { |
| 106 | $command = $this->_affinity['command']; |
| 107 | if ($command == self::BIND) { |
| 108 | if ($status->code != \Grpc\STATUS_OK) { |
| 109 | return; |
| 110 | } |
| 111 | $affinity_key = $this->getAffinityKeyFromProto($response); |
| 112 | $this->gcp_channel->bind($this->channel_ref, $affinity_key); |
| 113 | } elseif ($command == self::UNBIND) { |
| 114 | $this->gcp_channel->unbind($this->affinity_key); |
| 115 | } |
| 116 | } |
| 117 | $this->channel_ref->activeStreamRefDecr(); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Get the affinity key based on the affinity config. |
| 122 | * |
| 123 | * @param mixed $proto Objects may contain the affinity key. |
| 124 | * |
| 125 | * @return string Affinity key. |
| 126 | */ |
| 127 | protected function getAffinityKeyFromProto($proto) |
| 128 | { |
| 129 | if ($this->_affinity) { |
| 130 | $names = $this->_affinity['affinityKey']; |
| 131 | $names_arr = explode(".", $names); |
| 132 | foreach ($names_arr as $name) { |
| 133 | $getAttrMethod = 'get' . ucfirst($name); |
| 134 | $proto = call_user_func_array(array($proto, $getAttrMethod), array()); |
| 135 | } |
| 136 | return $proto; |
| 137 | } |
| 138 | echo "Cannot find the field in the proto\n"; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * @return mixed The metadata sent by the server |
| 143 | */ |
| 144 | public function getMetadata() |
| 145 | { |
| 146 | if (!$this->has_real_call) { |
| 147 | $this->createRealCall(); |
| 148 | $this->has_real_call = true; |
| 149 | } |
| 150 | return $this->real_call->getMetadata(); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * @return mixed The trailing metadata sent by the server |
| 155 | */ |
| 156 | public function getTrailingMetadata() |
| 157 | { |
| 158 | if (!$this->has_real_call) { |
| 159 | $this->createRealCall(); |
| 160 | $this->has_real_call = true; |
| 161 | } |
| 162 | return $this->real_call->getTrailingMetadata(); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * @return string The URI of the endpoint |
| 167 | */ |
| 168 | public function getPeer() |
| 169 | { |
| 170 | if (!$this->has_real_call) { |
| 171 | $this->createRealCall(); |
| 172 | $this->has_real_call = true; |
| 173 | } |
| 174 | return $this->real_call->getPeer(); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Cancels the call. |
| 179 | */ |
| 180 | public function cancel() |
| 181 | { |
| 182 | if (!$this->has_real_call) { |
| 183 | $this->has_real_call = true; |
| 184 | $this->createRealCall(); |
| 185 | } |
| 186 | $this->real_call->cancel(); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Serialize a message to the protobuf binary format. |
| 191 | * |
| 192 | * @param mixed $data The Protobuf message |
| 193 | * |
| 194 | * @return string The protobuf binary format |
| 195 | */ |
| 196 | protected function _serializeMessage($data) |
| 197 | { |
| 198 | return $this->real_call->_serializeMessage($data); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Deserialize a response value to an object. |
| 203 | * |
| 204 | * @param string $value The binary value to deserialize |
| 205 | * |
| 206 | * @return mixed The deserialized value |
| 207 | */ |
| 208 | protected function _deserializeResponse($value) |
| 209 | { |
| 210 | return $this->real_call->_deserializeResponse($value); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Set the CallCredentials for the underlying Call. |
| 215 | * |
| 216 | * @param CallCredentials $call_credentials The CallCredentials object |
| 217 | */ |
| 218 | public function setCallCredentials($call_credentials) |
| 219 | { |
| 220 | $this->call->setCredentials($call_credentials); |
| 221 | } |
| 222 | } |
| 223 |