Class DelayedStream
- java.lang.Object
-
- io.grpc.internal.DelayedStream
-
- All Implemented Interfaces:
ClientStream
,Stream
- Direct Known Subclasses:
DelayedClientTransport.PendingStream
class DelayedStream extends java.lang.Object implements ClientStream
A stream that queues requests before the transport is available, and delegates to a real stream implementation when the transport is available.ClientStream
itself doesn't require thread-safety. However, the state ofDelayedStream
may be internally altered by different threads, thus internal synchronization is necessary.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
DelayedStream.DelayedStreamListener
-
Field Summary
Fields Modifier and Type Field Description private DelayedStream.DelayedStreamListener
delayedListener
private Status
error
private ClientStreamListener
listener
Non-null
iff start has been called.private boolean
passThrough
true
once realStream is valid and all pending calls have been drained.private java.util.List<java.lang.Runnable>
pendingCalls
private java.util.List<java.lang.Runnable>
preStartPendingCalls
private ClientStream
realStream
Must holdthis
lock when setting.private long
startTimeNanos
private long
streamSetTimeNanos
-
Constructor Summary
Constructors Constructor Description DelayedStream()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
appendTimeoutInsight(InsightBuilder insight)
Append information that will be included in the locally generated DEADLINE_EXCEEDED errors to the givenInsightBuilder
, in order to tell the user about the state of the stream so that they can better diagnose the cause of the error.void
cancel(Status reason)
Abnormally terminates the stream.private void
delayOrExecute(java.lang.Runnable runnable)
Enqueue the runnable or execute it now.private void
drainPendingCalls()
Called to transitionpassThrough
totrue
.void
flush()
Flushes any internally buffered messages to the remote end-point.Attributes
getAttributes()
Attributes that the stream holds at the current moment.(package private) ClientStream
getRealStream()
void
halfClose()
Closes the local side of this stream and flushes any remaining messages.private void
internalStart(ClientStreamListener listener)
Starts stream without synchronization.boolean
isReady()
Iftrue
, indicates that the transport is capable of sending additional messages without requiring excessive buffering internally.protected void
onEarlyCancellation(Status reason)
void
optimizeForDirectExecutor()
Provides a hint that directExecutor is being used by the listener for callbacks to the application.void
request(int numMessages)
Requests up to the given number of messages from the call to be delivered viaStreamListener.messagesAvailable(StreamListener.MessageProducer)
.void
setAuthority(java.lang.String authority)
Override the default authority withauthority
.void
setCompressor(Compressor compressor)
Sets the compressor on the framer.void
setDeadline(Deadline deadline)
Sets the effective deadline of the RPC.void
setDecompressorRegistry(DecompressorRegistry decompressorRegistry)
Sets the registry to find a decompressor for the framer.void
setFullStreamDecompression(boolean fullStreamDecompression)
Enables full-stream decompression, allowing the client stream to useGzipInflatingBuffer
to decode inbound GZIP compressed streams.void
setMaxInboundMessageSize(int maxSize)
Sets the max size accepted from the remote endpoint.void
setMaxOutboundMessageSize(int maxSize)
Sets the max size sent to the remote endpoint.void
setMessageCompression(boolean enable)
Enables per-message compression, if an encoding type has been negotiated.private void
setRealStream(ClientStream realStream)
(package private) java.lang.Runnable
setStream(ClientStream stream)
Transfers all pending and future requests and mutations to the given stream.void
start(ClientStreamListener listener)
Starts stream.void
writeMessage(java.io.InputStream message)
Writes a message payload to the remote end-point.
-
-
-
Field Detail
-
passThrough
private volatile boolean passThrough
true
once realStream is valid and all pending calls have been drained.
-
listener
private ClientStreamListener listener
Non-null
iff start has been called. Used to assert methods are called in appropriate order, but also used if an error occurs beforerealStream
is set.
-
realStream
private ClientStream realStream
Must holdthis
lock when setting.
-
error
private Status error
-
pendingCalls
private java.util.List<java.lang.Runnable> pendingCalls
-
delayedListener
private DelayedStream.DelayedStreamListener delayedListener
-
startTimeNanos
private long startTimeNanos
-
streamSetTimeNanos
private long streamSetTimeNanos
-
preStartPendingCalls
private java.util.List<java.lang.Runnable> preStartPendingCalls
-
-
Method Detail
-
setMaxInboundMessageSize
public void setMaxInboundMessageSize(int maxSize)
Description copied from interface:ClientStream
Sets the max size accepted from the remote endpoint.- Specified by:
setMaxInboundMessageSize
in interfaceClientStream
-
setMaxOutboundMessageSize
public void setMaxOutboundMessageSize(int maxSize)
Description copied from interface:ClientStream
Sets the max size sent to the remote endpoint.- Specified by:
setMaxOutboundMessageSize
in interfaceClientStream
-
setDeadline
public void setDeadline(Deadline deadline)
Description copied from interface:ClientStream
Sets the effective deadline of the RPC.- Specified by:
setDeadline
in interfaceClientStream
-
appendTimeoutInsight
public void appendTimeoutInsight(InsightBuilder insight)
Description copied from interface:ClientStream
Append information that will be included in the locally generated DEADLINE_EXCEEDED errors to the givenInsightBuilder
, in order to tell the user about the state of the stream so that they can better diagnose the cause of the error.- Specified by:
appendTimeoutInsight
in interfaceClientStream
-
setStream
@CheckReturnValue final java.lang.Runnable setStream(ClientStream stream)
Transfers all pending and future requests and mutations to the given stream. Method will return quickly, but if the returned Runnable is non-null it must be called to complete the process. The Runnable may take a while to execute.No-op if either this method or
cancel(io.grpc.Status)
have already been called.
-
drainPendingCalls
private void drainPendingCalls()
Called to transitionpassThrough
totrue
. This method is not safe to be called multiple times; the caller must ensure it will only be called once, ever.this
lock should not be held when calling this method.
-
delayOrExecute
private void delayOrExecute(java.lang.Runnable runnable)
Enqueue the runnable or execute it now. Call sites that may be called many times may want avoid this method ifpassThrough == true
.Note that this method is no more thread-safe than
runnable
. It is thread-safe if and only ifrunnable
is thread-safe.
-
setAuthority
public void setAuthority(java.lang.String authority)
Description copied from interface:ClientStream
Override the default authority withauthority
. May only be called beforeClientStream.start(io.grpc.internal.ClientStreamListener)
.- Specified by:
setAuthority
in interfaceClientStream
-
start
public void start(ClientStreamListener listener)
Description copied from interface:ClientStream
Starts stream. This method may only be called once. It is safe to do latent initialization of the stream up untilClientStream.start(io.grpc.internal.ClientStreamListener)
is called.This method should not throw any exceptions.
- Specified by:
start
in interfaceClientStream
- Parameters:
listener
- non-null
listener of stream events
-
internalStart
private void internalStart(ClientStreamListener listener)
Starts stream without synchronization.listener
should be same instance aslistener
.
-
getAttributes
public Attributes getAttributes()
Description copied from interface:ClientStream
Attributes that the stream holds at the current moment. Thread-safe and can be called at any time, although some attributes are there only after a certain point.- Specified by:
getAttributes
in interfaceClientStream
-
writeMessage
public void writeMessage(java.io.InputStream message)
Description copied from interface:Stream
Writes a message payload to the remote end-point. The bytes from the stream are immediately read by the Transport. Where possible callers should use streams that areKnownLength
to improve efficiency. This method will always return immediately and will not wait for the write to complete. If the stream has astart()
method, it must be called before writing any messages.It is recommended that the caller consult
Stream.isReady()
before calling this method to avoid excessive buffering in the transport.This method takes ownership of the InputStream, and implementations are responsible for calling
InputStream.close()
.- Specified by:
writeMessage
in interfaceStream
- Parameters:
message
- stream containing the serialized message to be sent
-
flush
public void flush()
Description copied from interface:Stream
Flushes any internally buffered messages to the remote end-point.
-
cancel
public void cancel(Status reason)
Description copied from interface:ClientStream
Abnormally terminates the stream. After calling this method, no further messages will be sent or received, however it may still be possible to receive buffered messages for a brief period untilClientStreamListener.closed(io.grpc.Status, io.grpc.internal.ClientStreamListener.RpcProgress, io.grpc.Metadata)
is called. This method may only be called afterClientStream.start(io.grpc.internal.ClientStreamListener)
, but else is safe to be called at any time and multiple times and from any thread.- Specified by:
cancel
in interfaceClientStream
- Parameters:
reason
- must be non-OK
-
onEarlyCancellation
protected void onEarlyCancellation(Status reason)
-
setRealStream
private void setRealStream(ClientStream realStream)
-
halfClose
public void halfClose()
Description copied from interface:ClientStream
Closes the local side of this stream and flushes any remaining messages. After this is called, no further messages may be sent on this stream, but additional messages may be received until the remote end-point is closed. This method may only be called once, and only afterClientStream.start(io.grpc.internal.ClientStreamListener)
.- Specified by:
halfClose
in interfaceClientStream
-
request
public void request(int numMessages)
Description copied from interface:Stream
Requests up to the given number of messages from the call to be delivered viaStreamListener.messagesAvailable(StreamListener.MessageProducer)
. No additional messages will be delivered. If the stream has astart()
method, it must be called before requesting messages.
-
optimizeForDirectExecutor
public void optimizeForDirectExecutor()
Description copied from interface:Stream
Provides a hint that directExecutor is being used by the listener for callbacks to the application. No action is required. There is no requirement that this method actually matches the executor used.- Specified by:
optimizeForDirectExecutor
in interfaceStream
-
setCompressor
public void setCompressor(Compressor compressor)
Description copied from interface:Stream
Sets the compressor on the framer.- Specified by:
setCompressor
in interfaceStream
- Parameters:
compressor
- the compressor to use
-
setFullStreamDecompression
public void setFullStreamDecompression(boolean fullStreamDecompression)
Description copied from interface:ClientStream
Enables full-stream decompression, allowing the client stream to useGzipInflatingBuffer
to decode inbound GZIP compressed streams.- Specified by:
setFullStreamDecompression
in interfaceClientStream
-
setDecompressorRegistry
public void setDecompressorRegistry(DecompressorRegistry decompressorRegistry)
Description copied from interface:ClientStream
Sets the registry to find a decompressor for the framer. May only be called beforeClientStream.start(io.grpc.internal.ClientStreamListener)
. If the transport does not support compression, this may do nothing.- Specified by:
setDecompressorRegistry
in interfaceClientStream
- Parameters:
decompressorRegistry
- the registry of decompressors for decoding responses
-
isReady
public boolean isReady()
Description copied from interface:Stream
Iftrue
, indicates that the transport is capable of sending additional messages without requiring excessive buffering internally. Otherwise,StreamListener.onReady()
will be called when it turnstrue
.This is just a suggestion and the application is free to ignore it, however doing so may result in excessive buffering within the transport.
-
setMessageCompression
public void setMessageCompression(boolean enable)
Description copied from interface:Stream
Enables per-message compression, if an encoding type has been negotiated. If no message encoding has been negotiated, this is a no-op. By default per-message compression is enabled, but may not have any effect if compression is not enabled on the call.- Specified by:
setMessageCompression
in interfaceStream
-
getRealStream
ClientStream getRealStream()
-
-