| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* |
| 5 |
* Copyright 2018 gRPC authors. |
| 6 |
* |
| 7 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 |
* you may not use this file except in compliance with the License. |
| 9 |
* You may obtain a copy of the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
* |
| 13 |
* Unless required by applicable law or agreed to in writing, software |
| 14 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 15 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 |
* See the License for the specific language governing permissions and |
| 17 |
* limitations under the License. |
| 18 |
* |
| 19 |
*/ |
| 20 |
namespace Dudlewebs\WPMCS\Grpc\Gcp; |
| 21 |
|
| 22 |
/** |
| 23 |
* GCPCallInvoker updates the channel pool(GcpExtensionChannel) for |
| 24 |
* each RPC. The idea is: |
| 25 |
* Before the RPC starts, pick a channel from the channel pool: |
| 26 |
* - if the RPC is bound to a channel, use that channel. |
| 27 |
* - if the RPC doesn't bound to a channel, use the one with minimum active streams. |
| 28 |
* After the RPC finishes, update the active stream ref count. |
| 29 |
* - if the RPC is defined as bind, bind the channel with corresponding key like |
| 30 |
* spanner session name. |
| 31 |
* - if the RPC is defined as unbind, unbind the channel with the key. |
| 32 |
*/ |
| 33 |
class GCPCallInvoker implements \Dudlewebs\WPMCS\Grpc\CallInvoker |
| 34 |
{ |
| 35 |
private $channel; |
| 36 |
private $affinity_conf; |
| 37 |
/** |
| 38 |
* @param array $affinity_conf Store the affinity config for process each RPC. |
| 39 |
*/ |
| 40 |
public function __construct($affinity_conf) |
| 41 |
{ |
| 42 |
$this->affinity_conf = $affinity_conf; |
| 43 |
} |
| 44 |
/** |
| 45 |
* @param string $hostname |
| 46 |
* @param array $opts |
| 47 |
* @return GcpExtensionChannel |
| 48 |
*/ |
| 49 |
public function createChannelFactory($hostname, $opts) |
| 50 |
{ |
| 51 |
if ($this->channel) { |
| 52 |
// $call_invoker object has already created from previews PHP-FPM scripts. |
| 53 |
// Only need to update the $opts including the credentials. |
| 54 |
$this->channel->updateOpts($opts); |
| 55 |
} else { |
| 56 |
$opts['affinity_conf'] = $this->affinity_conf; |
| 57 |
$channel = new GcpExtensionChannel($hostname, $opts); |
| 58 |
$this->channel = $channel; |
| 59 |
} |
| 60 |
return $this->channel; |
| 61 |
} |
| 62 |
// _getChannel is used for testing only. |
| 63 |
public function GetChannel() |
| 64 |
{ |
| 65 |
return $this->channel; |
| 66 |
} |
| 67 |
public function UnaryCall($channel, $method, $deserialize, $options) |
| 68 |
{ |
| 69 |
return new GCPUnaryCall($channel, $method, $deserialize, $options); |
| 70 |
} |
| 71 |
public function ClientStreamingCall($channel, $method, $deserialize, $options) |
| 72 |
{ |
| 73 |
return new GCPClientStreamCall($channel, $method, $deserialize, $options); |
| 74 |
} |
| 75 |
public function ServerStreamingCall($channel, $method, $deserialize, $options) |
| 76 |
{ |
| 77 |
return new GCPServerStreamCall($channel, $method, $deserialize, $options); |
| 78 |
} |
| 79 |
public function BidiStreamingCall($channel, $method, $deserialize, $options) |
| 80 |
{ |
| 81 |
return new GCPBidiStreamingCall($channel, $method, $deserialize, $options); |
| 82 |
} |
| 83 |
} |
| 84 |
|