Package io.grpc.netty
Class NettyClientHandler
- java.lang.Object
-
- io.netty.channel.ChannelHandlerAdapter
-
- io.netty.channel.ChannelInboundHandlerAdapter
-
- io.netty.handler.codec.ByteToMessageDecoder
-
- io.netty.handler.codec.http2.Http2ConnectionHandler
-
- io.grpc.netty.GrpcHttp2ConnectionHandler
-
- io.grpc.netty.AbstractNettyHandler
-
- io.grpc.netty.NettyClientHandler
-
- All Implemented Interfaces:
io.netty.channel.ChannelHandler
,io.netty.channel.ChannelInboundHandler
,io.netty.channel.ChannelOutboundHandler
,io.netty.handler.codec.http2.Http2LifecycleManager
class NettyClientHandler extends AbstractNettyHandler
Client-side Netty handler for GRPC processing. All event handlers are executed entirely within the context of the Netty Channel thread.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
NettyClientHandler.FrameListener
private static class
NettyClientHandler.PingCountingFrameWriter
-
Nested classes/interfaces inherited from class io.grpc.netty.AbstractNettyHandler
AbstractNettyHandler.FlowControlPinger, AbstractNettyHandler.PingLimiter
-
-
Field Summary
Fields Modifier and Type Field Description private Status
abruptGoAwayStatus
private Attributes
attributes
private java.lang.String
authority
private Status
channelInactiveReason
private WriteQueue
clientWriteQueue
private Attributes
eagAttributes
private static Status
EXHAUSTED_STREAMS_STATUS
Status used when the transport has exhausted the number of streams.private InUseStateAggregator<io.netty.handler.codec.http2.Http2Stream>
inUseState
private KeepAliveManager
keepAliveManager
private ClientTransportLifecycleManager
lifecycleManager
private static java.util.logging.Logger
logger
(package private) static java.lang.Object
NOOP_MESSAGE
A message that simply passes through the channel without any real processing.private Http2Ping
ping
private InternalChannelz.Security
securityInfo
private com.google.common.base.Supplier<com.google.common.base.Stopwatch>
stopwatchFactory
private io.netty.handler.codec.http2.Http2Connection.PropertyKey
streamKey
private TransportTracer
transportTracer
private static long
USER_PING_PAYLOAD
-
Fields inherited from class io.grpc.netty.GrpcHttp2ConnectionHandler
ADAPTIVE_CUMULATOR, ADAPTIVE_CUMULATOR_COMPOSE_MIN_SIZE_DEFAULT, channelUnused
-
-
Constructor Summary
Constructors Modifier Constructor Description private
NettyClientHandler(io.netty.handler.codec.http2.Http2ConnectionDecoder decoder, io.netty.handler.codec.http2.Http2ConnectionEncoder encoder, io.netty.handler.codec.http2.Http2Settings settings, ChannelLogger negotiationLogger, ClientTransportLifecycleManager lifecycleManager, KeepAliveManager keepAliveManager, com.google.common.base.Supplier<com.google.common.base.Stopwatch> stopwatchFactory, java.lang.Runnable tooManyPingsRunnable, TransportTracer transportTracer, Attributes eagAttributes, java.lang.String authority, boolean autoFlowControl, AbstractNettyHandler.PingLimiter pingLimiter, com.google.common.base.Ticker ticker)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
cancelPing(java.lang.Throwable t)
private void
cancelStream(io.netty.channel.ChannelHandlerContext ctx, CancelClientStreamCommand cmd, io.netty.channel.ChannelPromise promise)
Cancels this stream.void
channelInactive(io.netty.channel.ChannelHandlerContext ctx)
Handler for the Channel shutting down.private NettyClientStream.TransportState
clientStream(io.netty.handler.codec.http2.Http2Stream stream)
Gets the client stream associated to the given HTTP/2 stream object.void
close(io.netty.channel.ChannelHandlerContext ctx, io.netty.channel.ChannelPromise promise)
private void
createStream(CreateStreamCommand command, io.netty.channel.ChannelPromise promise)
Attempts to create a new stream from the given command.private void
createStreamTraced(int streamId, NettyClientStream.TransportState stream, io.netty.handler.codec.http2.Http2Headers headers, boolean isGet, boolean shouldBeCountedForInUse, io.netty.channel.ChannelPromise promise)
private void
forcefulClose(io.netty.channel.ChannelHandlerContext ctx, ForcefulCloseCommand msg, io.netty.channel.ChannelPromise promise)
(package private) Attributes
getAttributes()
The protocol negotiation attributes, available once the protocol negotiation completes; otherwise returnsAttributes.EMPTY
.java.lang.String
getAuthority()
Returns the authority of the server.Attributes
getEagAttributes()
Get the attributes of the EquivalentAddressGroup used to create this transport.(package private) ClientTransportLifecycleManager
getLifecycleManager()
(package private) InternalChannelz.Security
getSecurityInfo()
(package private) WriteQueue
getWriteQueue()
private void
goingAway(long errorCode, byte[] debugData)
Handler for a GOAWAY being received.private void
gracefulClose(io.netty.channel.ChannelHandlerContext ctx, GracefulCloseCommand msg, io.netty.channel.ChannelPromise promise)
void
handleProtocolNegotiationCompleted(Attributes attributes, InternalChannelz.Security securityInfo)
Triggered on protocol negotiation completion.private int
incrementAndGetNextStreamId()
protected boolean
isGracefulShutdownComplete()
(package private) static NettyClientHandler
newHandler(ClientTransportLifecycleManager lifecycleManager, KeepAliveManager keepAliveManager, boolean autoFlowControl, int flowControlWindow, int maxHeaderListSize, com.google.common.base.Supplier<com.google.common.base.Stopwatch> stopwatchFactory, java.lang.Runnable tooManyPingsRunnable, TransportTracer transportTracer, Attributes eagAttributes, java.lang.String authority, ChannelLogger negotiationLogger, com.google.common.base.Ticker ticker)
(package private) static NettyClientHandler
newHandler(io.netty.handler.codec.http2.Http2Connection connection, io.netty.handler.codec.http2.Http2FrameReader frameReader, io.netty.handler.codec.http2.Http2FrameWriter frameWriter, ClientTransportLifecycleManager lifecycleManager, KeepAliveManager keepAliveManager, boolean autoFlowControl, int flowControlWindow, int maxHeaderListSize, com.google.common.base.Supplier<com.google.common.base.Stopwatch> stopwatchFactory, java.lang.Runnable tooManyPingsRunnable, TransportTracer transportTracer, Attributes eagAttributes, java.lang.String authority, ChannelLogger negotiationLogger, com.google.common.base.Ticker ticker)
protected void
onConnectionError(io.netty.channel.ChannelHandlerContext ctx, boolean outbound, java.lang.Throwable cause, io.netty.handler.codec.http2.Http2Exception http2Ex)
private void
onDataRead(int streamId, io.netty.buffer.ByteBuf data, int padding, boolean endOfStream)
Handler for an inbound HTTP/2 DATA frame.private void
onHeadersRead(int streamId, io.netty.handler.codec.http2.Http2Headers headers, boolean endStream)
private void
onRstStreamRead(int streamId, long errorCode)
Handler for an inbound HTTP/2 RST_STREAM frame, terminating a stream.protected void
onStreamError(io.netty.channel.ChannelHandlerContext ctx, boolean outbound, java.lang.Throwable cause, io.netty.handler.codec.http2.Http2Exception.StreamException http2Ex)
private io.netty.handler.codec.http2.Http2Stream
requireHttp2Stream(int streamId)
(package private) void
returnProcessedBytes(io.netty.handler.codec.http2.Http2Stream stream, int bytes)
Returns the given processed bytes back to inbound flow control.private void
sendGrpcFrame(io.netty.channel.ChannelHandlerContext ctx, SendGrpcFrameCommand cmd, io.netty.channel.ChannelPromise promise)
Sends the given GRPC frame for the stream.private void
sendPingFrame(io.netty.channel.ChannelHandlerContext ctx, SendPingCommand msg, io.netty.channel.ChannelPromise promise)
private void
sendPingFrameTraced(io.netty.channel.ChannelHandlerContext ctx, SendPingCommand msg, io.netty.channel.ChannelPromise promise)
Sends a PING frame.(package private) void
startWriteQueue(io.netty.channel.Channel channel)
private Status
statusFromH2Error(Status.Code statusCode, java.lang.String context, long errorCode, byte[] debugData)
IfstatusCode
is non-null, it will be used instead of the http2 error code mapping.void
write(io.netty.channel.ChannelHandlerContext ctx, java.lang.Object msg, io.netty.channel.ChannelPromise promise)
Handler for commands sent from the stream.(package private) static void
writeBufferingAndRemove(io.netty.channel.Channel channel)
-
Methods inherited from class io.grpc.netty.AbstractNettyHandler
channelActive, ctx, exceptionCaught, flowControlPing, handlerAdded, setAutoTuneFlowControl
-
Methods inherited from class io.grpc.netty.GrpcHttp2ConnectionHandler
getNegotiationLogger, handleProtocolNegotiationCompleted, notifyUnused, usingPre4_1_111_Netty
-
Methods inherited from class io.netty.handler.codec.http2.Http2ConnectionHandler
bind, channelReadComplete, channelWritabilityChanged, closeStream, closeStreamLocal, closeStreamRemote, connect, connection, decode, decoder, deregister, disconnect, encoder, flush, frameWriter, goAway, gracefulShutdownTimeoutMillis, gracefulShutdownTimeoutMillis, handlerRemoved0, handleServerHeaderDecodeSizeError, onError, onHttpClientUpgrade, onHttpServerUpgrade, read, resetStream
-
Methods inherited from class io.netty.handler.codec.ByteToMessageDecoder
actualReadableBytes, callDecode, channelRead, decodeLast, discardSomeReadBytes, handlerRemoved, internalBuffer, isSingleDecode, setCumulator, setDiscardAfterReads, setSingleDecode, userEventTriggered
-
Methods inherited from class io.netty.channel.ChannelInboundHandlerAdapter
channelRegistered, channelUnregistered
-
-
-
-
Field Detail
-
logger
private static final java.util.logging.Logger logger
-
NOOP_MESSAGE
static final java.lang.Object NOOP_MESSAGE
A message that simply passes through the channel without any real processing. It is useful to check if buffers have been drained and test the health of the channel in a single operation.
-
EXHAUSTED_STREAMS_STATUS
private static final Status EXHAUSTED_STREAMS_STATUS
Status used when the transport has exhausted the number of streams.
-
USER_PING_PAYLOAD
private static final long USER_PING_PAYLOAD
- See Also:
- Constant Field Values
-
streamKey
private final io.netty.handler.codec.http2.Http2Connection.PropertyKey streamKey
-
lifecycleManager
private final ClientTransportLifecycleManager lifecycleManager
-
keepAliveManager
private final KeepAliveManager keepAliveManager
-
stopwatchFactory
private final com.google.common.base.Supplier<com.google.common.base.Stopwatch> stopwatchFactory
-
transportTracer
private final TransportTracer transportTracer
-
eagAttributes
private final Attributes eagAttributes
-
authority
private final java.lang.String authority
-
inUseState
private final InUseStateAggregator<io.netty.handler.codec.http2.Http2Stream> inUseState
-
clientWriteQueue
private WriteQueue clientWriteQueue
-
ping
private Http2Ping ping
-
attributes
private Attributes attributes
-
securityInfo
private InternalChannelz.Security securityInfo
-
abruptGoAwayStatus
private Status abruptGoAwayStatus
-
channelInactiveReason
private Status channelInactiveReason
-
-
Constructor Detail
-
NettyClientHandler
private NettyClientHandler(io.netty.handler.codec.http2.Http2ConnectionDecoder decoder, io.netty.handler.codec.http2.Http2ConnectionEncoder encoder, io.netty.handler.codec.http2.Http2Settings settings, ChannelLogger negotiationLogger, ClientTransportLifecycleManager lifecycleManager, KeepAliveManager keepAliveManager, com.google.common.base.Supplier<com.google.common.base.Stopwatch> stopwatchFactory, java.lang.Runnable tooManyPingsRunnable, TransportTracer transportTracer, Attributes eagAttributes, java.lang.String authority, boolean autoFlowControl, AbstractNettyHandler.PingLimiter pingLimiter, com.google.common.base.Ticker ticker)
-
-
Method Detail
-
newHandler
static NettyClientHandler newHandler(ClientTransportLifecycleManager lifecycleManager, @Nullable KeepAliveManager keepAliveManager, boolean autoFlowControl, int flowControlWindow, int maxHeaderListSize, com.google.common.base.Supplier<com.google.common.base.Stopwatch> stopwatchFactory, java.lang.Runnable tooManyPingsRunnable, TransportTracer transportTracer, Attributes eagAttributes, java.lang.String authority, ChannelLogger negotiationLogger, com.google.common.base.Ticker ticker)
-
newHandler
static NettyClientHandler newHandler(io.netty.handler.codec.http2.Http2Connection connection, io.netty.handler.codec.http2.Http2FrameReader frameReader, io.netty.handler.codec.http2.Http2FrameWriter frameWriter, ClientTransportLifecycleManager lifecycleManager, KeepAliveManager keepAliveManager, boolean autoFlowControl, int flowControlWindow, int maxHeaderListSize, com.google.common.base.Supplier<com.google.common.base.Stopwatch> stopwatchFactory, java.lang.Runnable tooManyPingsRunnable, TransportTracer transportTracer, Attributes eagAttributes, java.lang.String authority, ChannelLogger negotiationLogger, com.google.common.base.Ticker ticker)
-
getAttributes
Attributes getAttributes()
The protocol negotiation attributes, available once the protocol negotiation completes; otherwise returnsAttributes.EMPTY
.
-
write
public void write(io.netty.channel.ChannelHandlerContext ctx, java.lang.Object msg, io.netty.channel.ChannelPromise promise) throws java.lang.Exception
Handler for commands sent from the stream.- Specified by:
write
in interfaceio.netty.channel.ChannelOutboundHandler
- Overrides:
write
in classio.netty.handler.codec.http2.Http2ConnectionHandler
- Throws:
java.lang.Exception
-
startWriteQueue
void startWriteQueue(io.netty.channel.Channel channel)
-
getWriteQueue
WriteQueue getWriteQueue()
-
getLifecycleManager
ClientTransportLifecycleManager getLifecycleManager()
-
returnProcessedBytes
void returnProcessedBytes(io.netty.handler.codec.http2.Http2Stream stream, int bytes)
Returns the given processed bytes back to inbound flow control.
-
onHeadersRead
private void onHeadersRead(int streamId, io.netty.handler.codec.http2.Http2Headers headers, boolean endStream)
-
onDataRead
private void onDataRead(int streamId, io.netty.buffer.ByteBuf data, int padding, boolean endOfStream)
Handler for an inbound HTTP/2 DATA frame.
-
onRstStreamRead
private void onRstStreamRead(int streamId, long errorCode)
Handler for an inbound HTTP/2 RST_STREAM frame, terminating a stream.
-
close
public void close(io.netty.channel.ChannelHandlerContext ctx, io.netty.channel.ChannelPromise promise) throws java.lang.Exception
- Specified by:
close
in interfaceio.netty.channel.ChannelOutboundHandler
- Overrides:
close
in classio.netty.handler.codec.http2.Http2ConnectionHandler
- Throws:
java.lang.Exception
-
channelInactive
public void channelInactive(io.netty.channel.ChannelHandlerContext ctx) throws java.lang.Exception
Handler for the Channel shutting down.- Specified by:
channelInactive
in interfaceio.netty.channel.ChannelInboundHandler
- Overrides:
channelInactive
in classio.netty.handler.codec.http2.Http2ConnectionHandler
- Throws:
java.lang.Exception
-
handleProtocolNegotiationCompleted
public void handleProtocolNegotiationCompleted(Attributes attributes, InternalChannelz.Security securityInfo)
Description copied from class:GrpcHttp2ConnectionHandler
Triggered on protocol negotiation completion.It must me called after negotiation is completed but before given handler is added to the channel.
- Overrides:
handleProtocolNegotiationCompleted
in classGrpcHttp2ConnectionHandler
- Parameters:
attributes
- arbitrary attributes passed after protocol negotiation (eg. SSLSession).securityInfo
- informs channelz about the security protocol.
-
writeBufferingAndRemove
static void writeBufferingAndRemove(io.netty.channel.Channel channel)
-
getEagAttributes
public Attributes getEagAttributes()
Description copied from class:GrpcHttp2ConnectionHandler
Get the attributes of the EquivalentAddressGroup used to create this transport.- Overrides:
getEagAttributes
in classGrpcHttp2ConnectionHandler
-
getAuthority
public java.lang.String getAuthority()
Description copied from class:GrpcHttp2ConnectionHandler
Returns the authority of the server. Only available on the client-side.- Overrides:
getAuthority
in classGrpcHttp2ConnectionHandler
-
getSecurityInfo
InternalChannelz.Security getSecurityInfo()
-
onConnectionError
protected void onConnectionError(io.netty.channel.ChannelHandlerContext ctx, boolean outbound, java.lang.Throwable cause, io.netty.handler.codec.http2.Http2Exception http2Ex)
- Overrides:
onConnectionError
in classio.netty.handler.codec.http2.Http2ConnectionHandler
-
onStreamError
protected void onStreamError(io.netty.channel.ChannelHandlerContext ctx, boolean outbound, java.lang.Throwable cause, io.netty.handler.codec.http2.Http2Exception.StreamException http2Ex)
- Overrides:
onStreamError
in classio.netty.handler.codec.http2.Http2ConnectionHandler
-
isGracefulShutdownComplete
protected boolean isGracefulShutdownComplete()
- Overrides:
isGracefulShutdownComplete
in classio.netty.handler.codec.http2.Http2ConnectionHandler
-
createStream
private void createStream(CreateStreamCommand command, io.netty.channel.ChannelPromise promise) throws java.lang.Exception
Attempts to create a new stream from the given command. If there are too many active streams, the creation request is queued.- Throws:
java.lang.Exception
-
createStreamTraced
private void createStreamTraced(int streamId, NettyClientStream.TransportState stream, io.netty.handler.codec.http2.Http2Headers headers, boolean isGet, boolean shouldBeCountedForInUse, io.netty.channel.ChannelPromise promise)
-
cancelStream
private void cancelStream(io.netty.channel.ChannelHandlerContext ctx, CancelClientStreamCommand cmd, io.netty.channel.ChannelPromise promise)
Cancels this stream.
-
sendGrpcFrame
private void sendGrpcFrame(io.netty.channel.ChannelHandlerContext ctx, SendGrpcFrameCommand cmd, io.netty.channel.ChannelPromise promise)
Sends the given GRPC frame for the stream.
-
sendPingFrame
private void sendPingFrame(io.netty.channel.ChannelHandlerContext ctx, SendPingCommand msg, io.netty.channel.ChannelPromise promise)
-
sendPingFrameTraced
private void sendPingFrameTraced(io.netty.channel.ChannelHandlerContext ctx, SendPingCommand msg, io.netty.channel.ChannelPromise promise)
Sends a PING frame. If a ping operation is already outstanding, the callback in the message is registered to be called when the existing operation completes, and no new frame is sent.
-
gracefulClose
private void gracefulClose(io.netty.channel.ChannelHandlerContext ctx, GracefulCloseCommand msg, io.netty.channel.ChannelPromise promise) throws java.lang.Exception
- Throws:
java.lang.Exception
-
forcefulClose
private void forcefulClose(io.netty.channel.ChannelHandlerContext ctx, ForcefulCloseCommand msg, io.netty.channel.ChannelPromise promise) throws java.lang.Exception
- Throws:
java.lang.Exception
-
goingAway
private void goingAway(long errorCode, byte[] debugData)
Handler for a GOAWAY being received. Fails any streams created after the last known stream. May only be called during a read.
-
cancelPing
private void cancelPing(java.lang.Throwable t)
-
statusFromH2Error
private Status statusFromH2Error(Status.Code statusCode, java.lang.String context, long errorCode, byte[] debugData)
IfstatusCode
is non-null, it will be used instead of the http2 error code mapping.
-
clientStream
private NettyClientStream.TransportState clientStream(io.netty.handler.codec.http2.Http2Stream stream)
Gets the client stream associated to the given HTTP/2 stream object.
-
incrementAndGetNextStreamId
private int incrementAndGetNextStreamId() throws StatusException
- Throws:
StatusException
-
requireHttp2Stream
private io.netty.handler.codec.http2.Http2Stream requireHttp2Stream(int streamId)
-
-