Class DelayedClientTransport
- java.lang.Object
-
- io.grpc.internal.DelayedClientTransport
-
- All Implemented Interfaces:
ClientTransport
,ManagedClientTransport
,InternalInstrumented<InternalChannelz.SocketStats>
,InternalWithLogId
final class DelayedClientTransport extends java.lang.Object implements ManagedClientTransport
A client transport that queues requests before a real transport is available. Whenreprocess(io.grpc.LoadBalancer.SubchannelPicker)
is called, this class applies the providedLoadBalancer.SubchannelPicker
to pick a transport for each pending stream.This transport owns every stream that it has created until a real transport has been picked for that stream, at which point the ownership of the stream is transferred to the real transport, thus the delayed transport stops owning the stream.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
DelayedClientTransport.PendingStream
(package private) static class
DelayedClientTransport.PickerState
-
Nested classes/interfaces inherited from interface io.grpc.internal.ClientTransport
ClientTransport.PingCallback
-
Nested classes/interfaces inherited from interface io.grpc.internal.ManagedClientTransport
ManagedClientTransport.Listener
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.concurrent.Executor
defaultAppExecutor
private ManagedClientTransport.Listener
listener
private java.lang.Object
lock
private InternalLogId
logId
private java.util.Collection<DelayedClientTransport.PendingStream>
pendingStreams
private DelayedClientTransport.PickerState
pickerState
Immutable state needed for picking.private java.lang.Runnable
reportTransportInUse
private java.lang.Runnable
reportTransportNotInUse
private java.lang.Runnable
reportTransportTerminated
private SynchronizationContext
syncContext
-
Constructor Summary
Constructors Constructor Description DelayedClientTransport(java.util.concurrent.Executor defaultAppExecutor, SynchronizationContext syncContext)
Creates a new delayed transport.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private DelayedClientTransport.PendingStream
createPendingStream(LoadBalancer.PickSubchannelArgs args, ClientStreamTracer[] tracers)
Caller must callsyncContext.drain()
outside of lock because this method may schedule tasks on syncContext.InternalLogId
getLogId()
Returns an ID that is primarily used in debug logs.(package private) int
getPendingStreamsCount()
com.google.common.util.concurrent.ListenableFuture<InternalChannelz.SocketStats>
getStats()
Returns the stats object.boolean
hasPendingStreams()
ClientStream
newStream(MethodDescriptor<?,?> method, Metadata headers, CallOptions callOptions, ClientStreamTracer[] tracers)
If aLoadBalancer.SubchannelPicker
is being, or has been provided viareprocess(io.grpc.LoadBalancer.SubchannelPicker)
, the last picker will be consulted.void
ping(ClientTransport.PingCallback callback, java.util.concurrent.Executor executor)
Pings a remote endpoint.(package private) void
reprocess(LoadBalancer.SubchannelPicker picker)
Use the picker to try picking a transport for every pending stream, proceed the stream if the pick is successful, otherwise keep it pending.void
shutdown(Status status)
Prevents creating any new streams.void
shutdownNow(Status status)
Shuts down this transport and cancels all streams that it owns, hence immediately terminates this transport.java.lang.Runnable
start(ManagedClientTransport.Listener listener)
Starts transport.
-
-
-
Field Detail
-
logId
private final InternalLogId logId
-
lock
private final java.lang.Object lock
-
defaultAppExecutor
private final java.util.concurrent.Executor defaultAppExecutor
-
syncContext
private final SynchronizationContext syncContext
-
reportTransportInUse
private java.lang.Runnable reportTransportInUse
-
reportTransportNotInUse
private java.lang.Runnable reportTransportNotInUse
-
reportTransportTerminated
private java.lang.Runnable reportTransportTerminated
-
listener
private ManagedClientTransport.Listener listener
-
pendingStreams
@Nonnull private java.util.Collection<DelayedClientTransport.PendingStream> pendingStreams
-
pickerState
private volatile DelayedClientTransport.PickerState pickerState
Immutable state needed for picking. 'lock' must be held for writing.
-
-
Constructor Detail
-
DelayedClientTransport
DelayedClientTransport(java.util.concurrent.Executor defaultAppExecutor, SynchronizationContext syncContext)
Creates a new delayed transport.- Parameters:
defaultAppExecutor
- pending streams will create real streams and run buffered operations in an application executor, which will be this executor, unless there is on provided inCallOptions
.syncContext
- all listener callbacks of the delayed transport will be run from this SynchronizationContext.
-
-
Method Detail
-
start
public final java.lang.Runnable start(ManagedClientTransport.Listener listener)
Description copied from interface:ManagedClientTransport
Starts transport. This method may only be called once.This method and the returned
Runnable
should not throw any exceptions.- Specified by:
start
in interfaceManagedClientTransport
- Parameters:
listener
- non-null
listener of transport events- Returns:
- a
Runnable
that is executed after-the-fact by the original caller, typically after locks are released
-
newStream
public final ClientStream newStream(MethodDescriptor<?,?> method, Metadata headers, CallOptions callOptions, ClientStreamTracer[] tracers)
If aLoadBalancer.SubchannelPicker
is being, or has been provided viareprocess(io.grpc.LoadBalancer.SubchannelPicker)
, the last picker will be consulted.Otherwise, if the delayed transport is not shutdown, then a
DelayedClientTransport.PendingStream
is returned; if the transport is shutdown, then aFailingClientStream
is returned.- Specified by:
newStream
in interfaceClientTransport
- Parameters:
method
- the descriptor of the remote method to be called for this stream.headers
- to send at the beginning of the callcallOptions
- runtime options of the calltracers
- a non-empty array of tracers. The last element in it is reserved to be set by the load balancer's pick result and otherwise is a no-op tracer.- Returns:
- the newly created stream.
-
createPendingStream
private DelayedClientTransport.PendingStream createPendingStream(LoadBalancer.PickSubchannelArgs args, ClientStreamTracer[] tracers)
Caller must callsyncContext.drain()
outside of lock because this method may schedule tasks on syncContext.
-
ping
public final void ping(ClientTransport.PingCallback callback, java.util.concurrent.Executor executor)
Description copied from interface:ClientTransport
Pings a remote endpoint. When an acknowledgement is received, the given callback will be invoked using the given executor.Pings are not necessarily sent to the same endpoint, thus a successful ping only means at least one endpoint responded, but doesn't imply the availability of other endpoints (if there is any).
This is an optional method. Transports that do not have any mechanism by which to ping the remote endpoint may throw
UnsupportedOperationException
.- Specified by:
ping
in interfaceClientTransport
-
getStats
public com.google.common.util.concurrent.ListenableFuture<InternalChannelz.SocketStats> getStats()
Description copied from interface:InternalInstrumented
Returns the stats object.- Specified by:
getStats
in interfaceInternalInstrumented<InternalChannelz.SocketStats>
-
shutdown
public final void shutdown(Status status)
Prevents creating any new streams. Buffered streams are not failed and may still proceed whenreprocess(io.grpc.LoadBalancer.SubchannelPicker)
is called. The delayed transport will be terminated when there is no more buffered streams.- Specified by:
shutdown
in interfaceManagedClientTransport
-
shutdownNow
public final void shutdownNow(Status status)
Shuts down this transport and cancels all streams that it owns, hence immediately terminates this transport.- Specified by:
shutdownNow
in interfaceManagedClientTransport
-
hasPendingStreams
public final boolean hasPendingStreams()
-
getPendingStreamsCount
final int getPendingStreamsCount()
-
reprocess
final void reprocess(@Nullable LoadBalancer.SubchannelPicker picker)
Use the picker to try picking a transport for every pending stream, proceed the stream if the pick is successful, otherwise keep it pending.This method may be called concurrently with
newStream()
, and it's safe. All pending streams will be served by the latest picker (if a same picker is given more than once, they are considered different pickers) as soon as possible.This method must not be called concurrently with itself.
-
getLogId
public InternalLogId getLogId()
Description copied from interface:InternalWithLogId
Returns an ID that is primarily used in debug logs. It usually contains the class name and a numeric ID that is unique among the instances.The subclasses of this interface usually want to include the log ID in their
Object.toString()
results.- Specified by:
getLogId
in interfaceInternalWithLogId
-
-