Skip to content

Commit 2158b82

Browse files
feat(Spanner): Add the grpc keepalive configuration (#8863)
1 parent cabed91 commit 2158b82

2 files changed

Lines changed: 63 additions & 1 deletion

File tree

Spanner/src/SpannerClient.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ class SpannerClient
118118

119119
const FULL_CONTROL_SCOPE = 'https://www.googleapis.com/auth/spanner.data';
120120
const ADMIN_SCOPE = 'https://www.googleapis.com/auth/spanner.admin';
121+
private const GRPC_KEEPALIVE_MILLISECONDS = 120 * 1000;
121122

122123
private const SERVICE_NAME = 'google.spanner.v1.Spanner';
123124

@@ -204,7 +205,7 @@ public function __construct(array $options = [])
204205
'directedReadOptions' => [],
205206
'isolationLevel' => IsolationLevel::ISOLATION_LEVEL_UNSPECIFIED,
206207
'routeToLeader' => true,
207-
'cacheItemPool' => null,
208+
'cacheItemPool' => null
208209
];
209210

210211
$this->returnInt64AsObject = $options['returnInt64AsObject'];
@@ -213,6 +214,8 @@ public function __construct(array $options = [])
213214
$this->defaultQueryOptions = $options['queryOptions'];
214215
$this->isolationLevel = $options['isolationLevel'];
215216

217+
$options = $this->configureKeepAlive($options);
218+
216219
// Configure GAPIC client options
217220
$options = $this->buildClientOptions($options);
218221
if (isset($options['credentialsConfig']['scopes'])) {
@@ -1002,4 +1005,23 @@ private function getChannelId(array $options): int
10021005
// We have seen this channel, get the ID assigned to the channel
10031006
return self::$activeChannels[$channelObjectId];
10041007
}
1008+
1009+
/**
1010+
* Configures the GRPC KeepAlive time
1011+
*
1012+
* @param array $config The Configuration array
1013+
* @return array<mixed>
1014+
*/
1015+
private function configureKeepAlive(array $config): array
1016+
{
1017+
if (!isset($config['transportConfig']['grpc']['stubOpts'])) {
1018+
$config['transportConfig']['grpc']['stubOpts'] = [];
1019+
}
1020+
1021+
$config['transportConfig']['grpc']['stubOpts'] += [
1022+
'grpc.keepalive_time_ms' => self::GRPC_KEEPALIVE_MILLISECONDS
1023+
];
1024+
1025+
return $config;
1026+
}
10051027
}

Spanner/tests/Unit/SpannerClientTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,4 +770,44 @@ private function getTotalChannel(SpannerClient $client): int
770770

771771
return $property->getValue($client);
772772
}
773+
774+
public function testConfigureKeepAlive()
775+
{
776+
$client = new SpannerClient([
777+
'projectId' => 'test-project',
778+
'credentials' => Fixtures::KEYFILE_STUB_FIXTURE(),
779+
]);
780+
$reflection = new ReflectionClass($client);
781+
$method = $reflection->getMethod('configureKeepAlive');
782+
$method->setAccessible(true);
783+
784+
$config = [];
785+
$newConfig = $method->invoke($client, $config);
786+
787+
$this->assertArrayHasKey('transportConfig', $newConfig);
788+
$this->assertArrayHasKey('grpc', $newConfig['transportConfig']);
789+
$this->assertArrayHasKey('stubOpts', $newConfig['transportConfig']['grpc']);
790+
$this->assertArrayHasKey('grpc.keepalive_time_ms', $newConfig['transportConfig']['grpc']['stubOpts']);
791+
$this->assertEquals(
792+
120 * 1000, // 120 seconds x 1000
793+
$newConfig['transportConfig']['grpc']['stubOpts']['grpc.keepalive_time_ms']
794+
);
795+
796+
// Test that it doesn't overwrite existing config
797+
$config = [
798+
'transportConfig' => [
799+
'grpc' => [
800+
'stubOpts' => [
801+
'foo' => 'bar'
802+
]
803+
]
804+
]
805+
];
806+
$newConfig = $method->invoke($client, $config);
807+
$this->assertEquals('bar', $newConfig['transportConfig']['grpc']['stubOpts']['foo']);
808+
$this->assertEquals(
809+
120 * 1000, // 120 seconds x 1000
810+
$newConfig['transportConfig']['grpc']['stubOpts']['grpc.keepalive_time_ms']
811+
);
812+
}
773813
}

0 commit comments

Comments
 (0)