Skip to content

Commit

Permalink
update code style
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhouyihaiDing committed Jun 29, 2018
1 parent 612fe82 commit 2f2a95e
Show file tree
Hide file tree
Showing 18 changed files with 757 additions and 256 deletions.
14 changes: 14 additions & 0 deletions .php_cs.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
return PhpCsFixer\Config::create()
->setRules([
'@PSR2' => true,
'concat_space' => ['spacing' => 'one'],
'no_unused_imports' => true,
'method_argument_space' => false,
])
->setFinder(
PhpCsFixer\Finder::create()
->in(__DIR__)
)
;

24 changes: 11 additions & 13 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
{
"name": "grpc/grpc-gcp",
"type": "library",
"description": "gcp extension for gRPC PHP",
"keywords": ["rpc"],
"homepage": "https://grpc.io",
"name": "google/cloud-grpc",
"description": "gRPC gcp library for channel management",
"license": "Apache-2.0",
"version": "0.0.1",
"config": {
"preferred-install": "source"
},
"require": {
"php": ">=5.5.0",
"grpc/grpc": ">=1.13.0"
"google/protobuf": "^v3.3.0",
"grpc/grpc": "dev-master",
"psr/cache": "^1.0.1"
},
"require-dev": {
"google/auth": "v0.9"
},
"suggest": {
"ext-protobuf": "For better performance, install the protobuf C extension.",
"google/protobuf": "To get started using grpc quickly, install the native protobuf library."
},
"autoload": {
"psr-4": {
"Grpc\\GCP\\": "src/lib"
"Google\\Cloud\\Grpc\\": "src/",
"": ["src/generated/"]
}
}
}

61 changes: 29 additions & 32 deletions src/lib/ChannelRef.php → src/ChannelRef.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
* limitations under the License.
*
*/
namespace Grpc\GCP;
namespace Google\Cloud\Grpc;

class _ChannelRef
/**
* This is a private class
*/
class ChannelRef
{
// $opts has all information except Credentials for creating a Grpc\Channel
// except Grpc\Credentials.
Expand All @@ -39,13 +42,14 @@ public function __construct($target, $channel_id, $opts, $affinity_ref=0, $activ
$this->affinity_ref = $affinity_ref;
$this->active_stream_ref = $active_stream_ref;
$this->opts = $opts;
$this->has_deserialized = new _HasDeserialized();
$this->has_deserialized = new CreatedByDeserializeCheck();
}

public function getRealChannel($credentials) {
public function getRealChannel($credentials)
{
// TODO(ddyihai): remove this check once the serialzer handler for
// \Grpc\Channel is implemented.
if(!$this->has_deserialized->getData()) {
if (!$this->has_deserialized->getData()) {
return $this->real_channel;
}
// If this ChannelRef is created by deserialization, $real_channel is invalid
Expand All @@ -56,7 +60,7 @@ public function getRealChannel($credentials) {
// new connection.

// 'credentials' in the array $opts will be unset during creating the channel.
if(!array_key_exists('credentials', $this->opts)){
if (!array_key_exists('credentials', $this->opts)) {
$this->opts['credentials'] = $credentials;
}
$real_channel = new \Grpc\Channel($this->target, $this->opts);
Expand All @@ -66,35 +70,28 @@ public function getRealChannel($credentials) {
return $real_channel;
}

public function getAffinityRef() {return $this->affinity_ref;}
public function getActiveStreamRef() {return $this->active_stream_ref;}
public function affinityRefIncr() {$this->affinity_ref += 1;}
public function affinityRefDecr() {$this->affinity_ref -= 1;}
public function activeStreamRefIncr() {$this->active_stream_ref += 1;}
public function activeStreamRefDecr() {$this->active_stream_ref -= 1;}
}

// _HasDeserialized is used to check whether _ChannelRef is created by deserialization or not.
// If it is, $real_channel is invalid thus we need to recreate it using $opts.
// If not, we can use $real_channel directly instead of creating a new one.
// It is useful to handle 'force_new' channel option.
// TODO(ddyihai): remove it once the serialzer handler for \Grpc\Channel is implemented.
class _HasDeserialized implements \Serializable {
private $data;
public function __construct() {
$this->data = 1;
public function getAffinityRef()
{
return $this->affinity_ref;
}
public function getActiveStreamRef()
{
return $this->active_stream_ref;
}
public function serialize() {
return '0';
public function affinityRefIncr()
{
$this->affinity_ref += 1;
}
public function unserialize($data) {
$this->data = 1;
public function affinityRefDecr()
{
$this->affinity_ref -= 1;
}
public function setData($data) {
$this->data = $data;
public function activeStreamRefIncr()
{
$this->active_stream_ref += 1;
}
public function getData() {
return $this->data;
public function activeStreamRefDecr()
{
$this->active_stream_ref -= 1;
}
}

18 changes: 10 additions & 8 deletions src/lib/GCPConfig.php → src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* limitations under the License.
*
*/
namespace Grpc\GCP;
namespace Google\Cloud\Grpc;

use Psr\Cache\CacheItemPoolInterface;

Expand All @@ -25,21 +25,22 @@ class Config
private $hostname;
private $gcp_call_invoker;

public function __construct($hostname, $conf, CacheItemPoolInterface $cacheItemPool = null)
public function __construct($hostname, $conf = null, CacheItemPoolInterface $cacheItemPool = null)
{
if($conf == null) {
if ($conf == null) {
// If there is no configure file, use the default gRPC channel.
$this->gcp_call_invoker = new \Grpc\DefaultCallInvoker();
return;
}
$gcp_channel = null;
// $hostname is used for distinguishing different cloud APIs.
$this->hostname = $hostname;
$channel_pool_key = $hostname.'gcp_channel' . getmypid();
$channel_pool_key = $hostname . 'gcp_channel' . getmypid();
if (!$cacheItemPool) {
// If there is no cacheItemPool, use shared memory for
// caching the configuration and channel pool.
$channel_pool_key = intval(base_convert(sha1($channel_pool_key), 16, 10));
$shm_id = shm_attach(getmypid());
$shm_id = shm_attach(getmypid(), 200000, 0600);
$var1 = @shm_get_var($shm_id, $channel_pool_key);
if ($var1) {
$gcp_call_invoker = unserialize($var1);
Expand Down Expand Up @@ -74,11 +75,13 @@ public function __construct($hostname, $conf, CacheItemPoolInterface $cacheItemP
}
}

public function callInvoker() {
public function callInvoker()
{
return $this->gcp_call_invoker;
}

private function parseConfObject($conf_object) {
private function parseConfObject($conf_object)
{
$config = json_decode($conf_object->serializeToJsonString(), true);
$affinity_conf['channelPool'] = $config['channelPool'];
$aff_by_method = array();
Expand All @@ -94,4 +97,3 @@ private function parseConfObject($conf_object) {
return $affinity_conf;
}
}

52 changes: 52 additions & 0 deletions src/CreatedByDeserializeCheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/*
*
* Copyright 2018 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
namespace Google\Cloud\Grpc;

/**
* DeserializeCheck is used to check whether _ChannelRef is created by deserialization or not.
* If it is, $real_channel is invalid thus we need to recreate it using $opts.
* If not, we can use $real_channel directly instead of creating a new one.
* It is useful to handle 'force_new' channel option.
* This is a private class
*/
class CreatedByDeserializeCheck implements \Serializable
{
// TODO(ddyihai): remove it once the serialzer handler for \Grpc\Channel is implemented.
private $data;
public function __construct()
{
$this->data = 1;
}
public function serialize()
{
return '0';
}
public function unserialize($data)
{
$this->data = 1;
}
public function setData($data)
{
$this->data = $data;
}
public function getData()
{
return $this->data;
}
}
Loading

0 comments on commit 2f2a95e

Please sign in to comment.