Class FlushConsolidationHandler
- java.lang.Object
-
- io.netty.channel.ChannelHandlerAdapter
-
- io.netty.channel.ChannelInboundHandlerAdapter
-
- io.netty.channel.ChannelDuplexHandler
-
- io.netty.handler.flush.FlushConsolidationHandler
-
- All Implemented Interfaces:
ChannelHandler,ChannelInboundHandler,ChannelOutboundHandler
public class FlushConsolidationHandler extends ChannelDuplexHandler
ChannelDuplexHandlerwhich consolidatesChannel.flush()/ChannelHandlerContext.flush()operations (which also includesChannel.writeAndFlush(Object)/Channel.writeAndFlush(Object, ChannelPromise)andChannelOutboundInvoker.writeAndFlush(Object)/ChannelOutboundInvoker.writeAndFlush(Object, ChannelPromise)).Flush operations are generally speaking expensive as these may trigger a syscall on the transport level. Thus it is in most cases (where write latency can be traded with throughput) a good idea to try to minimize flush operations as much as possible.
If a read loop is currently ongoing,
flush(ChannelHandlerContext)will not be passed on to the nextChannelOutboundHandlerin theChannelPipeline, as it will pick up any pending flushes whenchannelReadComplete(ChannelHandlerContext)is triggered. If no read loop is ongoing, the behavior depends on theconsolidateWhenNoReadInProgressconstructor argument:- if
false, flushes are passed on to the next handler directly; - if
true, the invocation of the next handler is submitted as a separate task on the event loop. Under high throughput, this gives the opportunity to process other flushes before the task gets executed, thus batching multiple flushes into one.
explicitFlushAfterFlushesis reached the flush will be forwarded as well (whether while in a read loop, or while batching outside of a read loop).If the
Channelbecomes non-writable it will also try to execute any pending flush operations.The
FlushConsolidationHandlershould be put as firstChannelHandlerin theChannelPipelineto have the best effect.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
-
Field Summary
Fields Modifier and Type Field Description static intDEFAULT_EXPLICIT_FLUSH_AFTER_FLUSHESThe default number of flushes after which a flush will be forwarded to downstream handlers (whether while in a read loop, or while batching outside of a read loop).
-
Constructor Summary
Constructors Constructor Description FlushConsolidationHandler()Create new instance which explicit flush after 256 pending flush operations at the latest.FlushConsolidationHandler(int explicitFlushAfterFlushes)Create new instance which doesn't consolidate flushes when no read is in progress.FlushConsolidationHandler(int explicitFlushAfterFlushes, boolean consolidateWhenNoReadInProgress)Create new instance.
-
Method Summary
-
Methods inherited from class io.netty.channel.ChannelDuplexHandler
bind, connect, deregister, read, write
-
Methods inherited from class io.netty.channel.ChannelInboundHandlerAdapter
channelActive, channelInactive, channelRegistered, channelUnregistered, userEventTriggered
-
Methods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, isSharable
-
-
-
-
Field Detail
-
DEFAULT_EXPLICIT_FLUSH_AFTER_FLUSHES
public static final int DEFAULT_EXPLICIT_FLUSH_AFTER_FLUSHES
The default number of flushes after which a flush will be forwarded to downstream handlers (whether while in a read loop, or while batching outside of a read loop).- See Also:
- Constant Field Values
-
-
Constructor Detail
-
FlushConsolidationHandler
public FlushConsolidationHandler()
Create new instance which explicit flush after 256 pending flush operations at the latest.
-
FlushConsolidationHandler
public FlushConsolidationHandler(int explicitFlushAfterFlushes)
Create new instance which doesn't consolidate flushes when no read is in progress.- Parameters:
explicitFlushAfterFlushes- the number of flushes after which an explicit flush will be done.
-
FlushConsolidationHandler
public FlushConsolidationHandler(int explicitFlushAfterFlushes, boolean consolidateWhenNoReadInProgress)Create new instance.- Parameters:
explicitFlushAfterFlushes- the number of flushes after which an explicit flush will be done.consolidateWhenNoReadInProgress- whether to consolidate flushes even when no read loop is currently ongoing.
-
-
Method Detail
-
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
-
flush
public void flush(ChannelHandlerContext ctx) throws java.lang.Exception
Description copied from class:ChannelDuplexHandlerCallsChannelHandlerContext.flush()to forward to the nextChannelOutboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
flushin interfaceChannelOutboundHandler- Overrides:
flushin classChannelDuplexHandler- Parameters:
ctx- theChannelHandlerContextfor which the flush operation is made- Throws:
java.lang.Exception- thrown if an error occurs
-
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
-
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
-
exceptionCaught
public void exceptionCaught(ChannelHandlerContext ctx, java.lang.Throwable cause) throws java.lang.Exception
Description copied from class:ChannelInboundHandlerAdapterCallsChannelHandlerContext.fireExceptionCaught(Throwable)to forward to the nextChannelHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
exceptionCaughtin interfaceChannelHandler- Specified by:
exceptionCaughtin interfaceChannelInboundHandler- Overrides:
exceptionCaughtin classChannelInboundHandlerAdapter- Throws:
java.lang.Exception
-
disconnect
public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws java.lang.Exception
Description copied from class:ChannelDuplexHandlerCallsChannelOutboundInvoker.disconnect(ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
disconnectin interfaceChannelOutboundHandler- Overrides:
disconnectin classChannelDuplexHandler- Parameters:
ctx- theChannelHandlerContextfor which the disconnect operation is madepromise- theChannelPromiseto notify once the operation completes- Throws:
java.lang.Exception- thrown if an error occurs
-
close
public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws java.lang.Exception
Description copied from class:ChannelDuplexHandlerCallsChannelOutboundInvoker.close(ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
closein interfaceChannelOutboundHandler- Overrides:
closein classChannelDuplexHandler- Parameters:
ctx- theChannelHandlerContextfor which the close operation is madepromise- theChannelPromiseto notify once the operation completes- Throws:
java.lang.Exception- thrown if an error occurs
-
channelWritabilityChanged
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws java.lang.Exception
Description copied from class:ChannelInboundHandlerAdapterCallsChannelHandlerContext.fireChannelWritabilityChanged()to forward to the nextChannelInboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
channelWritabilityChangedin interfaceChannelInboundHandler- Overrides:
channelWritabilityChangedin classChannelInboundHandlerAdapter- 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
-
-