Package io.netty.handler.flow
Class FlowControlHandler
- java.lang.Object
-
- io.netty.channel.ChannelHandlerAdapter
-
- io.netty.channel.ChannelInboundHandlerAdapter
-
- io.netty.channel.ChannelDuplexHandler
-
- io.netty.handler.flow.FlowControlHandler
-
- All Implemented Interfaces:
ChannelHandler,ChannelInboundHandler,ChannelOutboundHandler
public class FlowControlHandler extends ChannelDuplexHandler
TheFlowControlHandlerensures that only one message perread()is sent downstream.Classes such as
ByteToMessageDecoderorMessageToByteEncoderare free to emit as many events as they like for any given input. A channel's auto reading configuration doesn't usually apply in these scenarios. This is causing problems in downstreamChannelHandlers that would like to hold subsequent events while they're processing one event. It's a common problem with theHttpObjectDecoderthat will very often fire anHttpRequestthat is immediately followed by aLastHttpContentevent.{@code ChannelPipeline pipeline = ...; pipeline.addLast(new HttpServerCodec()); pipeline.addLast(new FlowControlHandler()); pipeline.addLast(new MyExampleHandler()); class MyExampleHandler extends ChannelInboundHandlerAdapter {- See Also:
ChannelConfig.setAutoRead(boolean)
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classFlowControlHandler.RecyclableArrayDequeA recyclableArrayDeque.-
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
-
Field Summary
Fields Modifier and Type Field Description private ChannelConfigconfigprivate booleandequeuingtruewhile adequeue(ChannelHandlerContext)loop is on the stack.private static InternalLoggerloggerprivate FlowControlHandler.RecyclableArrayDequequeueprivate booleanreleaseMessagesprivate intunsatisfiedReadsNumber of unsatisfied downstreamread()calls.
-
Constructor Summary
Constructors Constructor Description FlowControlHandler()FlowControlHandler(boolean releaseMessages)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidchannelInactive(ChannelHandlerContext ctx)CallsChannelHandlerContext.fireChannelInactive()to forward to the nextChannelInboundHandlerin theChannelPipeline.voidchannelRead(ChannelHandlerContext ctx, java.lang.Object msg)CallsChannelHandlerContext.fireChannelRead(Object)to forward to the nextChannelInboundHandlerin theChannelPipeline.voidchannelReadComplete(ChannelHandlerContext ctx)CallsChannelHandlerContext.fireChannelReadComplete()to forward to the nextChannelInboundHandlerin theChannelPipeline.private booleandequeue(ChannelHandlerContext ctx)Dequeues messages while auto-read is enabled or downstream reads are unsatisfied, and updatesunsatisfiedReadsaccordingly.private voiddestroy()Releases all messages and destroys theQueue.voidhandlerAdded(ChannelHandlerContext ctx)Do nothing by default, sub-classes may override this method.voidhandlerRemoved(ChannelHandlerContext ctx)Do nothing by default, sub-classes may override this method.(package private) booleanisQueueEmpty()Determine if the underlyingQueueis empty.voidread(ChannelHandlerContext ctx)CallsChannelHandlerContext.read()to forward to the nextChannelOutboundHandlerin theChannelPipeline.-
Methods inherited from class io.netty.channel.ChannelDuplexHandler
bind, close, connect, deregister, disconnect, flush, write
-
Methods inherited from class io.netty.channel.ChannelInboundHandlerAdapter
channelActive, channelRegistered, channelUnregistered, channelWritabilityChanged, exceptionCaught, userEventTriggered
-
Methods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, isSharable
-
-
-
-
Field Detail
-
logger
private static final InternalLogger logger
-
releaseMessages
private final boolean releaseMessages
-
queue
private FlowControlHandler.RecyclableArrayDeque queue
-
config
private ChannelConfig config
-
unsatisfiedReads
private int unsatisfiedReads
Number of unsatisfied downstreamread()calls. A downstreamread()is considered unsatisfied if auto-read is off and if it has not yet been paired with afireChannelReador a cumulativefireChannelReadComplete.A
read()can be satisfied in three ways, whichever comes first:- inside the
read()call itself, bydequeue()ing a message - in a
channelRead() - in a
channelReadComplete()
read()can be satisfied with auto-read on.When one or more
read()calls are unsatisfied, a downstreamchannelReadCompleteis fired only when either of the following happens:- auto-read is off and
unsatisfiedReadsreturns to zero afterdequeue()ing, or - an upstream
channelReadCompletearrives
- inside the
-
dequeuing
private boolean dequeuing
truewhile adequeue(ChannelHandlerContext)loop is on the stack.
-
-
Method Detail
-
isQueueEmpty
boolean isQueueEmpty()
Determine if the underlyingQueueis empty. This method exists for testing, debugging and inspection purposes and it is not Thread safe!
-
destroy
private void destroy()
Releases all messages and destroys theQueue.
-
handlerAdded
public void handlerAdded(ChannelHandlerContext ctx) throws java.lang.Exception
Description copied from class:ChannelHandlerAdapterDo nothing by default, sub-classes may override this method.- Specified by:
handlerAddedin interfaceChannelHandler- Overrides:
handlerAddedin classChannelHandlerAdapter- Throws:
java.lang.Exception
-
handlerRemoved
public void handlerRemoved(ChannelHandlerContext ctx) throws java.lang.Exception
Description copied from class:ChannelHandlerAdapterDo nothing by default, sub-classes may override this method.- Specified by:
handlerRemovedin interfaceChannelHandler- Overrides:
handlerRemovedin classChannelHandlerAdapter- Throws:
java.lang.Exception
-
channelInactive
public void channelInactive(ChannelHandlerContext ctx) throws java.lang.Exception
Description copied from class:ChannelInboundHandlerAdapterCallsChannelHandlerContext.fireChannelInactive()to forward to the nextChannelInboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
channelInactivein interfaceChannelInboundHandler- Overrides:
channelInactivein classChannelInboundHandlerAdapter- Throws:
java.lang.Exception
-
read
public void read(ChannelHandlerContext ctx) throws java.lang.Exception
Description copied from class:ChannelDuplexHandlerCallsChannelHandlerContext.read()to forward to the nextChannelOutboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
readin interfaceChannelOutboundHandler- Overrides:
readin classChannelDuplexHandler- Throws:
java.lang.Exception
-
channelRead
public void channelRead(ChannelHandlerContext ctx, java.lang.Object msg) throws java.lang.Exception
Description copied from class:ChannelInboundHandlerAdapterCallsChannelHandlerContext.fireChannelRead(Object)to forward to the nextChannelInboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
channelReadin interfaceChannelInboundHandler- Overrides:
channelReadin classChannelInboundHandlerAdapter- Throws:
java.lang.Exception
-
channelReadComplete
public void channelReadComplete(ChannelHandlerContext ctx) throws java.lang.Exception
Description copied from class:ChannelInboundHandlerAdapterCallsChannelHandlerContext.fireChannelReadComplete()to forward to the nextChannelInboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
channelReadCompletein interfaceChannelInboundHandler- Overrides:
channelReadCompletein classChannelInboundHandlerAdapter- Throws:
java.lang.Exception
-
dequeue
private boolean dequeue(ChannelHandlerContext ctx)
Dequeues messages while auto-read is enabled or downstream reads are unsatisfied, and updatesunsatisfiedReadsaccordingly.
-
-