Class GrpcCallContext
- java.lang.Object
-
- com.google.api.gax.grpc.GrpcCallContext
-
- All Implemented Interfaces:
RetryingContext
,ApiCallContext
@BetaApi("Reference ApiCallContext instead - this class is likely to experience breaking changes") public final class GrpcCallContext extends java.lang.Object implements ApiCallContext
GrpcCallContext encapsulates context data used to make a grpc call.GrpcCallContext is immutable in the sense that none of its methods modifies the GrpcCallContext itself or the underlying data. Methods of the form
withX
, such aswithTransportChannel(com.google.api.gax.rpc.TransportChannel)
, return copies of the object, but with one field changed. The immutability and thread safety of the arguments solely depends on the arguments themselves.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface com.google.api.gax.rpc.ApiCallContext
ApiCallContext.Key<T>
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static GrpcCallContext
createDefault()
Returns an empty instance with a null channel and defaultCallOptions
.boolean
equals(java.lang.Object o)
io.grpc.CallOptions
getCallOptions()
TheCallOptions
set on this context.io.grpc.Channel
getChannel()
TheChannel
set on this context.java.lang.Integer
getChannelAffinity()
The channel affinity for this context.java.util.Map<java.lang.String,java.util.List<java.lang.String>>
getExtraHeaders()
The extra header for this context.<T> T
getOption(ApiCallContext.Key<T> key)
Return the api call context option set for this context.java.util.Set<StatusCode.Code>
getRetryableCodes()
Returns the retryable codes to use with this context, ornull
if the default retryable codes should be used.RetrySettings
getRetrySettings()
Returns theRetrySettings
to use with this context, ornull
if the defaultRetrySettings
should be used.org.threeten.bp.Duration
getStreamIdleTimeout()
The stream idle timeout set for this context.org.threeten.bp.Duration
getStreamWaitTimeout()
The stream wait timeout set for this context.org.threeten.bp.Duration
getTimeout()
Returns the configured per-RPC timeout.ApiTracer
getTracer()
TheApiTracer
that was previously set for this context.int
hashCode()
ApiCallContext
merge(ApiCallContext inputCallContext)
For any values ininputCallContext
that are not null, override the corresponding values in the present instance.GrpcCallContext
nullToSelf(ApiCallContext inputContext)
Returns inputContext cast toGrpcCallContext
, or an emptyGrpcCallContext
if inputContext is null.static GrpcCallContext
of(io.grpc.Channel channel, io.grpc.CallOptions callOptions)
Returns an instance with the given channel andCallOptions
.GrpcCallContext
withCallOptions(io.grpc.CallOptions newCallOptions)
Returns a new instance with the call options set to the given call options.GrpcCallContext
withChannel(io.grpc.Channel newChannel)
Returns a new instance with the channel set to the given channel.GrpcCallContext
withChannelAffinity(java.lang.Integer affinity)
GrpcCallContext
withCredentials(com.google.auth.Credentials newCredentials)
Returns a new ApiCallContext with the given credentials set.GrpcCallContext
withExtraHeaders(java.util.Map<java.lang.String,java.util.List<java.lang.String>> extraHeaders)
Return a new ApiCallContext with the extraHeaders merged into the present instance.<T> GrpcCallContext
withOption(ApiCallContext.Key<T> key, T value)
Return a new ApiCallContext with additional option merged into the present instance.GrpcCallContext
withRequestParamsDynamicHeaderOption(java.lang.String requestParams)
GrpcCallContext
withRetryableCodes(java.util.Set<StatusCode.Code> retryableCodes)
Returns a new ApiCallContext with the given retryable codes set.GrpcCallContext
withRetrySettings(RetrySettings retrySettings)
Returns a new ApiCallContext with the givenRetrySettings
set.GrpcCallContext
withStreamIdleTimeout(org.threeten.bp.Duration streamIdleTimeout)
Returns a new ApiCallContext with the given stream idle timeout set.GrpcCallContext
withStreamWaitTimeout(org.threeten.bp.Duration streamWaitTimeout)
Returns a new ApiCallContext with the given stream timeout set.GrpcCallContext
withTimeout(org.threeten.bp.Duration timeout)
Returns a new ApiCallContext with the given timeout set.GrpcCallContext
withTracer(ApiTracer tracer)
Returns a newApiCallContext
with the givenApiTracer
.GrpcCallContext
withTransportChannel(TransportChannel inputChannel)
Returns a new ApiCallContext with the given channel set.
-
-
-
Method Detail
-
createDefault
public static GrpcCallContext createDefault()
Returns an empty instance with a null channel and defaultCallOptions
.
-
of
public static GrpcCallContext of(io.grpc.Channel channel, io.grpc.CallOptions callOptions)
Returns an instance with the given channel andCallOptions
.
-
nullToSelf
public GrpcCallContext nullToSelf(ApiCallContext inputContext)
Returns inputContext cast toGrpcCallContext
, or an emptyGrpcCallContext
if inputContext is null.- Specified by:
nullToSelf
in interfaceApiCallContext
- Parameters:
inputContext
- theApiCallContext
to cast if it is not null
-
withCredentials
public GrpcCallContext withCredentials(com.google.auth.Credentials newCredentials)
Description copied from interface:ApiCallContext
Returns a new ApiCallContext with the given credentials set.- Specified by:
withCredentials
in interfaceApiCallContext
-
withTransportChannel
public GrpcCallContext withTransportChannel(TransportChannel inputChannel)
Description copied from interface:ApiCallContext
Returns a new ApiCallContext with the given channel set.- Specified by:
withTransportChannel
in interfaceApiCallContext
-
withTimeout
public GrpcCallContext withTimeout(@Nullable org.threeten.bp.Duration timeout)
Description copied from interface:ApiCallContext
Returns a new ApiCallContext with the given timeout set.This sets the maximum amount of time a single unary RPC attempt can take. If retries are enabled, then this can take much longer, as each RPC attempt will have the same constant timeout. Unlike a deadline, timeouts are relative durations that are measure from the beginning of each RPC attempt. Please note that this limits the duration of a server streaming RPC as well.
If a method has default
RetrySettings
, the max attempts and/or total timeout is still respected when scheduling each RPC attempt.- Specified by:
withTimeout
in interfaceApiCallContext
-
getTimeout
@Nullable public org.threeten.bp.Duration getTimeout()
Description copied from interface:ApiCallContext
Returns the configured per-RPC timeout.- Specified by:
getTimeout
in interfaceApiCallContext
-
withStreamWaitTimeout
public GrpcCallContext withStreamWaitTimeout(@Nullable org.threeten.bp.Duration streamWaitTimeout)
Description copied from interface:ApiCallContext
Returns a new ApiCallContext with the given stream timeout set.This timeout only applies to a
ServerStreamingCallable
s. It limits the maximum amount of time that can pass between demand being signaled viaStreamController.request(int)
and actual message delivery toResponseObserver.onResponse(Object)
. Or, in the case of automatic flow control, since the last message was delivered toResponseObserver.onResponse(Object)
. This is useful to detect server or connection stalls. When the timeout has been reached, the stream will be closed with a retryableWatchdogTimeoutException
and a status ofStatusCode.Code.ABORTED
.A value of
Duration.ZERO
, disables the streaming wait timeout and a null value will use the default in the callable.Please note that this timeout is best effort and the maximum resolution is configured in
StubSettings.getStreamWatchdogCheckInterval()
.- Specified by:
withStreamWaitTimeout
in interfaceApiCallContext
-
withStreamIdleTimeout
public GrpcCallContext withStreamIdleTimeout(@Nullable org.threeten.bp.Duration streamIdleTimeout)
Description copied from interface:ApiCallContext
Returns a new ApiCallContext with the given stream idle timeout set.This timeout only applies to a
ServerStreamingCallable
s. It limits the maximum amount of timeout that can pass between a message being received byResponseObserver.onResponse(Object)
and demand being signaled viaStreamController.request(int)
. Please note that this timeout is best effort and the maximum resolution configured inStubSettings.getStreamWatchdogCheckInterval()
. This is useful to clean up streams that were partially read but never closed. When the timeout has been reached, the stream will be closed with a nonretryableWatchdogTimeoutException
and a status ofStatusCode.Code.ABORTED
.A value of
Duration.ZERO
, disables the streaming idle timeout and a null value will use the default in the callable.Please note that this timeout is best effort and the maximum resolution is configured in
StubSettings.getStreamWatchdogCheckInterval()
.- Specified by:
withStreamIdleTimeout
in interfaceApiCallContext
-
withChannelAffinity
@BetaApi("The surface for channel affinity is not stable yet and may change in the future.") public GrpcCallContext withChannelAffinity(@Nullable java.lang.Integer affinity)
-
withExtraHeaders
@BetaApi("The surface for extra headers is not stable yet and may change in the future.") public GrpcCallContext withExtraHeaders(java.util.Map<java.lang.String,java.util.List<java.lang.String>> extraHeaders)
Description copied from interface:ApiCallContext
Return a new ApiCallContext with the extraHeaders merged into the present instance.- Specified by:
withExtraHeaders
in interfaceApiCallContext
-
getRetrySettings
public RetrySettings getRetrySettings()
Description copied from interface:RetryingContext
Returns theRetrySettings
to use with this context, ornull
if the defaultRetrySettings
should be used.- Specified by:
getRetrySettings
in interfaceRetryingContext
-
withRetrySettings
public GrpcCallContext withRetrySettings(RetrySettings retrySettings)
Description copied from interface:ApiCallContext
Returns a new ApiCallContext with the givenRetrySettings
set.This sets the
RetrySettings
to use for the RPC. These settings will work in combination with either the default retryable codes for the RPC, or the retryable codes supplied throughApiCallContext.withRetryableCodes(Set)
. CallingApiCallContext.withRetrySettings(RetrySettings)
on an RPC that does not includeStatusCode.Code.DEADLINE_EXCEEDED
as one of its retryable codes (or without callingApiCallContext.withRetryableCodes(Set)
with a set that includes at leastStatusCode.Code.DEADLINE_EXCEEDED
) will effectively only set a single timeout that is equal toRetrySettings.getInitialRpcTimeout()
. If this timeout is exceeded, the RPC will not be retried and will fail withStatusCode.Code.DEADLINE_EXCEEDED
.Example usage:
Setting a logical call timeout for the context can be done similarly withApiCallContext context = GrpcCallContext.createDefault() .withRetrySettings(RetrySettings.newBuilder() .setInitialRetryDelay(Duration.ofMillis(10L)) .setInitialRpcTimeout(Duration.ofMillis(100L)) .setMaxAttempts(10) .setMaxRetryDelay(Duration.ofSeconds(10L)) .setMaxRpcTimeout(Duration.ofSeconds(30L)) .setRetryDelayMultiplier(1.4) .setRpcTimeoutMultiplier(1.5) .setTotalTimeout(Duration.ofMinutes(10L)) .build()) .withRetryableCodes(Sets.newSet( StatusCode.Code.UNAVAILABLE, StatusCode.Code.DEADLINE_EXCEEDED));
RetrySettings.Builder.setLogicalTimeout(Duration timeout)
.Example usage:
ApiCallContext context = GrpcCallContext.createDefault() .withRetrySettings(RetrySettings.newBuilder() .setInitialRetryDelay(Duration.ofMillis(10L)) .setMaxRetryDelay(Duration.ofSeconds(10L)) .setRetryDelayMultiplier(1.4) .setMaxAttempts(10) .setLogicalTimeout(Duration.ofSeconds(30L)) .build());
- Specified by:
withRetrySettings
in interfaceApiCallContext
-
getRetryableCodes
public java.util.Set<StatusCode.Code> getRetryableCodes()
Description copied from interface:RetryingContext
Returns the retryable codes to use with this context, ornull
if the default retryable codes should be used.- Specified by:
getRetryableCodes
in interfaceRetryingContext
-
withRetryableCodes
public GrpcCallContext withRetryableCodes(java.util.Set<StatusCode.Code> retryableCodes)
Description copied from interface:ApiCallContext
Returns a new ApiCallContext with the given retryable codes set.This sets the retryable codes to use for the RPC. These settings will work in combination with either the default
RetrySettings
for the RPC, or theRetrySettings
supplied throughApiCallContext.withRetrySettings(RetrySettings)
.Setting a non-empty set of retryable codes for an RPC that is not already retryable by default, will not have any effect and the RPC will NOT be retried. This option can only be used to change which codes are considered retryable for an RPC that already has at least one retryable code in its default settings.
- Specified by:
withRetryableCodes
in interfaceApiCallContext
-
merge
public ApiCallContext merge(ApiCallContext inputCallContext)
Description copied from interface:ApiCallContext
For any values ininputCallContext
that are not null, override the corresponding values in the present instance.- Specified by:
merge
in interfaceApiCallContext
-
getChannel
public io.grpc.Channel getChannel()
TheChannel
set on this context.
-
getCallOptions
public io.grpc.CallOptions getCallOptions()
TheCallOptions
set on this context.
-
getStreamWaitTimeout
@Nullable public org.threeten.bp.Duration getStreamWaitTimeout()
The stream wait timeout set for this context.- Specified by:
getStreamWaitTimeout
in interfaceApiCallContext
- See Also:
ApiCallContext.withStreamWaitTimeout(Duration)
-
getStreamIdleTimeout
@Nullable public org.threeten.bp.Duration getStreamIdleTimeout()
The stream idle timeout set for this context.- Specified by:
getStreamIdleTimeout
in interfaceApiCallContext
- See Also:
ApiCallContext.withStreamIdleTimeout(Duration)
-
getChannelAffinity
@Nullable public java.lang.Integer getChannelAffinity()
The channel affinity for this context.
-
getExtraHeaders
@BetaApi("The surface for extra headers is not stable yet and may change in the future.") public java.util.Map<java.lang.String,java.util.List<java.lang.String>> getExtraHeaders()
The extra header for this context.- Specified by:
getExtraHeaders
in interfaceApiCallContext
-
withChannel
public GrpcCallContext withChannel(io.grpc.Channel newChannel)
Returns a new instance with the channel set to the given channel.
-
withCallOptions
public GrpcCallContext withCallOptions(io.grpc.CallOptions newCallOptions)
Returns a new instance with the call options set to the given call options.
-
withRequestParamsDynamicHeaderOption
public GrpcCallContext withRequestParamsDynamicHeaderOption(java.lang.String requestParams)
-
getTracer
@Nonnull public ApiTracer getTracer()
TheApiTracer
that was previously set for this context.The
ApiTracer
will be used to trace the current operation and to annotate various events like retries.- Specified by:
getTracer
in interfaceApiCallContext
- Specified by:
getTracer
in interfaceRetryingContext
-
withTracer
public GrpcCallContext withTracer(@Nonnull ApiTracer tracer)
Returns a newApiCallContext
with the givenApiTracer
.The
ApiTracer
will be used to trace the current operation and to annotate various events like retries.- Specified by:
withTracer
in interfaceApiCallContext
- Parameters:
tracer
- theApiTracer
to set.
-
withOption
public <T> GrpcCallContext withOption(ApiCallContext.Key<T> key, T value)
Return a new ApiCallContext with additional option merged into the present instance. Any existing value of the key is overwritten.- Specified by:
withOption
in interfaceApiCallContext
-
getOption
public <T> T getOption(ApiCallContext.Key<T> key)
Return the api call context option set for this context.- Specified by:
getOption
in interfaceApiCallContext
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equals
in classjava.lang.Object
-
-