Skip to content

Commit 8919c5e

Browse files
authored
chore: create and reuse a base CallContext (#3816)
Every RPC that is executed by the client needs to create a new CallContext. Some of the settings in this CallContext is the same for all calls, and there is no need to create a new one completely from scratch for the properties that are the same for all calls. This change creates a base CallContext that is reused as the base for all RPCs.
1 parent 92494d0 commit 8919c5e

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

google-cloud-spanner/src/main/java/com/google/cloud/spanner/spi/v1/GapicSpannerRpc.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,8 @@ public class GapicSpannerRpc implements SpannerRpc {
280280

281281
private Supplier<Boolean> directPathEnabledSupplier = () -> false;
282282

283+
private final GrpcCallContext baseGrpcCallContext;
284+
283285
public static GapicSpannerRpc create(SpannerOptions options) {
284286
return new GapicSpannerRpc(options);
285287
}
@@ -333,6 +335,7 @@ public GapicSpannerRpc(final SpannerOptions options) {
333335
this.endToEndTracingEnabled = options.isEndToEndTracingEnabled();
334336
this.numChannels = options.getNumChannels();
335337
this.isGrpcGcpExtensionEnabled = options.isGrpcGcpExtensionEnabled();
338+
this.baseGrpcCallContext = createBaseCallContext();
336339

337340
if (initializeStubs) {
338341
// First check if SpannerOptions provides a TransportChannelProvider. Create one
@@ -1978,6 +1981,20 @@ private static <T> T get(final Future<T> future) throws SpannerException {
19781981
}
19791982
}
19801983

1984+
private GrpcCallContext createBaseCallContext() {
1985+
GrpcCallContext context = GrpcCallContext.createDefault();
1986+
if (compressorName != null) {
1987+
// This sets the compressor for Client -> Server.
1988+
context = context.withCallOptions(context.getCallOptions().withCompression(compressorName));
1989+
}
1990+
if (endToEndTracingEnabled) {
1991+
context = context.withExtraHeaders(metadataProvider.newEndToEndTracingHeader());
1992+
}
1993+
return context
1994+
.withStreamWaitTimeoutDuration(waitTimeout)
1995+
.withStreamIdleTimeoutDuration(idleTimeout);
1996+
}
1997+
19811998
// Before removing this method, please verify with a code owner that it is not used
19821999
// in any internal testing infrastructure.
19832000
@VisibleForTesting
@@ -2002,7 +2019,7 @@ <ReqT, RespT> GrpcCallContext newCallContext(
20022019
ReqT request,
20032020
MethodDescriptor<ReqT, RespT> method,
20042021
boolean routeToLeader) {
2005-
GrpcCallContext context = GrpcCallContext.createDefault();
2022+
GrpcCallContext context = this.baseGrpcCallContext;
20062023
if (options != null) {
20072024
if (this.isGrpcGcpExtensionEnabled) {
20082025
// Set channel affinity in gRPC-GCP.
@@ -2019,28 +2036,17 @@ <ReqT, RespT> GrpcCallContext newCallContext(
20192036
context = context.withChannelAffinity(Option.CHANNEL_HINT.getLong(options).intValue());
20202037
}
20212038
}
2022-
if (compressorName != null) {
2023-
// This sets the compressor for Client -> Server.
2024-
context = context.withCallOptions(context.getCallOptions().withCompression(compressorName));
2025-
}
20262039
context = context.withExtraHeaders(metadataProvider.newExtraHeaders(resource, projectName));
20272040
if (routeToLeader && leaderAwareRoutingEnabled) {
20282041
context = context.withExtraHeaders(metadataProvider.newRouteToLeaderHeader());
20292042
}
2030-
if (endToEndTracingEnabled) {
2031-
context = context.withExtraHeaders(metadataProvider.newEndToEndTracingHeader());
2032-
}
20332043
if (callCredentialsProvider != null) {
20342044
CallCredentials callCredentials = callCredentialsProvider.getCallCredentials();
20352045
if (callCredentials != null) {
20362046
context =
20372047
context.withCallOptions(context.getCallOptions().withCallCredentials(callCredentials));
20382048
}
20392049
}
2040-
context =
2041-
context
2042-
.withStreamWaitTimeoutDuration(waitTimeout)
2043-
.withStreamIdleTimeoutDuration(idleTimeout);
20442050
CallContextConfigurator configurator = SpannerOptions.CALL_CONTEXT_CONFIGURATOR_KEY.get();
20452051
ApiCallContext apiCallContextFromContext = null;
20462052
if (configurator != null) {

0 commit comments

Comments
 (0)