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
Config.php
114 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 | use Psr\Cache\CacheItemPoolInterface; |
| 22 | |
| 23 | /** |
| 24 | * Config is used to enable the support for the channel management. |
| 25 | */ |
| 26 | class Config |
| 27 | { |
| 28 | private $hostname; |
| 29 | private $gcp_call_invoker; |
| 30 | private $cross_script_shmem_enabled; |
| 31 | private $supported_sapis = ['fpm-fcgi', 'cli-server']; |
| 32 | |
| 33 | /** |
| 34 | * @param string $target The target API we want to manage the connection. |
| 35 | * @param \Grpc\Gcp\ApiConfig $conf |
| 36 | * @param CacheItemPoolInterface $cacheItemPool A pool for storing configuration and channels |
| 37 | * cross requests within a single worker process. |
| 38 | * @throws \RuntimeException When a failure occurs while attempting to attach to shared memory. |
| 39 | */ |
| 40 | public function __construct($target, $conf = null, CacheItemPoolInterface $cacheItemPool = null) |
| 41 | { |
| 42 | if ($conf == null) { |
| 43 | // If there is no configure file, use the default gRPC channel. |
| 44 | $this->gcp_call_invoker = new \Grpc\DefaultCallInvoker(); |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | $gcp_channel = null; |
| 49 | $url_host = parse_url($target, PHP_URL_HOST); |
| 50 | $this->hostname = $url_host ? $url_host : $target; |
| 51 | $channel_pool_key = $this->hostname . '.gcp.channel.' . getmypid(); |
| 52 | |
| 53 | if (!$cacheItemPool) { |
| 54 | $affinity_conf = $this->parseConfObject($conf); |
| 55 | $gcp_call_invoker = new GCPCallInvoker($affinity_conf); |
| 56 | $this->gcp_call_invoker = $gcp_call_invoker; |
| 57 | } else { |
| 58 | $item = $cacheItemPool->getItem($channel_pool_key); |
| 59 | if ($item->isHit()) { |
| 60 | // Channel pool for the $hostname API has already created. |
| 61 | $gcp_call_invoker = unserialize($item->get()); |
| 62 | } else { |
| 63 | $affinity_conf = $this->parseConfObject($conf); |
| 64 | // Create GCP channel based on the information. |
| 65 | $gcp_call_invoker = new GCPCallInvoker($affinity_conf); |
| 66 | } |
| 67 | $this->gcp_call_invoker = $gcp_call_invoker; |
| 68 | register_shutdown_function(function ($gcp_call_invoker, $cacheItemPool, $item) { |
| 69 | // Push the current gcp_channel back into the pool when the script finishes. |
| 70 | $item->set(serialize($gcp_call_invoker)); |
| 71 | $cacheItemPool->save($item); |
| 72 | }, $gcp_call_invoker, $cacheItemPool, $item); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @return \Grpc\CallInvoker The call invoker to be hooked into the gRPC |
| 78 | */ |
| 79 | public function callInvoker() |
| 80 | { |
| 81 | return $this->gcp_call_invoker; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * @return string The URI of the endpoint |
| 86 | */ |
| 87 | public function getTarget() |
| 88 | { |
| 89 | return $this->channel->getTarget(); |
| 90 | } |
| 91 | |
| 92 | private function parseConfObject($conf_object) |
| 93 | { |
| 94 | $config = json_decode($conf_object->serializeToJsonString(), true); |
| 95 | if (isset($config['channelPool'])) { |
| 96 | $affinity_conf['channelPool'] = $config['channelPool']; |
| 97 | } |
| 98 | $aff_by_method = array(); |
| 99 | if (isset($config['method'])) { |
| 100 | for ($i = 0; $i < count($config['method']); $i++) { |
| 101 | // In proto3, if the value is default, eg 0 for int, it won't be serialized. |
| 102 | // Thus serialized string may not have `command` if the value is default 0(BOUND). |
| 103 | if (!array_key_exists('command', $config['method'][$i]['affinity'])) { |
| 104 | $config['method'][$i]['affinity']['command'] = 'BOUND'; |
| 105 | } |
| 106 | $aff_by_method[$config['method'][$i]['name'][0]] = $config['method'][$i]['affinity']; |
| 107 | } |
| 108 | } |
| 109 | $affinity_conf['affinity_by_method'] = $aff_by_method; |
| 110 | return $affinity_conf; |
| 111 | } |
| 112 | |
| 113 | } |
| 114 |