diff --git a/Spanner/src/Connection/Grpc.php b/Spanner/src/Connection/Grpc.php index a39f74bee306..03489470aa8f 100644 --- a/Spanner/src/Connection/Grpc.php +++ b/Spanner/src/Connection/Grpc.php @@ -122,13 +122,13 @@ class Grpc implements ConnectionInterface */ private $longRunningGrpcClients; - private function enableConnectionManagement($conf_path) + private function enableConnectionManagement($hostName, $confPath) { // TODO(ddyihai): move this function to GrpcTrait, if we are going // to enable the grpc-gcp library for other apis. $conf = new ApiConfig(); - $conf->mergeFromJsonString($string = file_get_contents($conf_path)); - $config = new Config($conf); + $conf->mergeFromJsonString(file_get_contents($confPath)); + $config = new Config($hostName, $conf); return $config; } @@ -137,6 +137,10 @@ private function enableConnectionManagement($conf_path) */ public function __construct(array $config = []) { + if (getenv("ENABLE_GCP_OPTIMIZER") === false) { + // Enable the GCP optimizer by default. + putenv('ENABLE_GCP_OPTIMIZER=TRUE'); + } $this->serializer = new Serializer([ 'commit_timestamp' => function ($v) { return $this->formatTimestampFromApi($v); @@ -165,12 +169,11 @@ public function __construct(array $config = []) : null ); - if (isset($config['enableGcpOptimizer'])) { + if (getenv('ENABLE_GCP_OPTIMIZER') == 'TRUE') { $api = 'spanner'; - $conf_path = __DIR__. "/../$api.grpc.config"; - $grpc_gcp_config = $this->enableConnectionManagement($conf_path); - $grpcConfig['grpc_call_invoker'] = $grpc_gcp_config->callInvoker(); - unset($config['enableGcpOptimizer']); + $confPath = __DIR__. "/../$api.grpc.config"; + $grpcGcpConfig = $this->enableConnectionManagement('spanner.googleapis.com', $confPath); + $grpcConfig['grpc_call_invoker'] = $grpcGcpConfig->callInvoker(); } $this->spannerClient = isset($config['gapicSpannerClient']) diff --git a/composer.json b/composer.json index dedc5351275f..461271dd158e 100644 --- a/composer.json +++ b/composer.json @@ -51,7 +51,7 @@ "psr/http-message": "1.0.*", "ramsey/uuid": "~3", "google/gax": "^0.36", - "google/grpc-gcp": "^v0.1.0" + "google/grpc-gcp": "v0.1.0" }, "require-dev": { "phpunit/phpunit": "^4.8|^5.0",