@@ -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}
0 commit comments