Class FlushConsolidationHandler
- All Implemented Interfaces:
ChannelHandler
,ChannelInboundHandler
,ChannelOutboundHandler
ChannelDuplexHandler
which consolidates Channel.flush()
/ ChannelHandlerContext.flush()
operations (which also includes
ChannelOutboundInvoker.writeAndFlush(Object)
/ ChannelOutboundInvoker.writeAndFlush(Object, ChannelPromise)
and
ChannelOutboundInvoker.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 next
ChannelOutboundHandler
in the ChannelPipeline
, as it will pick up any pending flushes when
channelReadComplete(ChannelHandlerContext)
is triggered.
If no read loop is ongoing, the behavior depends on the consolidateWhenNoReadInProgress
constructor 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.
explicitFlushAfterFlushes
is reached the flush will be forwarded as well (whether while in a read loop, or
while batching outside of a read loop).
If the Channel
becomes non-writable it will also try to execute any pending flush operations.
The FlushConsolidationHandler
should be put as first ChannelHandler
in the
ChannelPipeline
to have the best effect.
-
Nested Class Summary
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final boolean
private ChannelHandlerContext
static final int
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).private final int
private int
private final Runnable
private Future
<?> private boolean
-
Constructor Summary
ConstructorsConstructorDescriptionCreate 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
Modifier and TypeMethodDescriptionprivate void
void
channelRead
(ChannelHandlerContext ctx, Object msg) CallsChannelHandlerContext.fireChannelRead(Object)
to forward to the nextChannelInboundHandler
in theChannelPipeline
.void
CallsChannelHandlerContext.fireChannelReadComplete()
to forward to the nextChannelInboundHandler
in theChannelPipeline
.void
CallsChannelHandlerContext.fireChannelWritabilityChanged()
to forward to the nextChannelInboundHandler
in theChannelPipeline
.void
close
(ChannelHandlerContext ctx, ChannelPromise promise) CallsChannelOutboundInvoker.close(ChannelPromise)
to forward to the nextChannelOutboundHandler
in theChannelPipeline
.void
disconnect
(ChannelHandlerContext ctx, ChannelPromise promise) CallsChannelOutboundInvoker.disconnect(ChannelPromise)
to forward to the nextChannelOutboundHandler
in theChannelPipeline
.void
exceptionCaught
(ChannelHandlerContext ctx, Throwable cause) CallsChannelHandlerContext.fireExceptionCaught(Throwable)
to forward to the nextChannelHandler
in theChannelPipeline
.void
CallsChannelHandlerContext.flush()
to forward to the nextChannelOutboundHandler
in theChannelPipeline
.private void
private void
void
Do nothing by default, sub-classes may override this method.void
Do nothing by default, sub-classes may override this method.private void
private void
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 Details
-
explicitFlushAfterFlushes
private final int explicitFlushAfterFlushes -
consolidateWhenNoReadInProgress
private final boolean consolidateWhenNoReadInProgress -
flushTask
-
flushPendingCount
private int flushPendingCount -
readInProgress
private boolean readInProgress -
ctx
-
nextScheduledFlush
-
DEFAULT_EXPLICIT_FLUSH_AFTER_FLUSHES
public static final int DEFAULT_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).- See Also:
-
-
Constructor Details
-
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 Details
-
handlerAdded
Description copied from class:ChannelHandlerAdapter
Do nothing by default, sub-classes may override this method.- Specified by:
handlerAdded
in interfaceChannelHandler
- Overrides:
handlerAdded
in classChannelHandlerAdapter
- Throws:
Exception
-
flush
Description copied from class:ChannelDuplexHandler
CallsChannelHandlerContext.flush()
to forward to the nextChannelOutboundHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
flush
in interfaceChannelOutboundHandler
- Overrides:
flush
in classChannelDuplexHandler
- Parameters:
ctx
- theChannelHandlerContext
for which the flush operation is made- Throws:
Exception
- thrown if an error occurs
-
channelReadComplete
Description copied from class:ChannelInboundHandlerAdapter
CallsChannelHandlerContext.fireChannelReadComplete()
to forward to the nextChannelInboundHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
channelReadComplete
in interfaceChannelInboundHandler
- Overrides:
channelReadComplete
in classChannelInboundHandlerAdapter
- Throws:
Exception
-
channelRead
Description copied from class:ChannelInboundHandlerAdapter
CallsChannelHandlerContext.fireChannelRead(Object)
to forward to the nextChannelInboundHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
channelRead
in interfaceChannelInboundHandler
- Overrides:
channelRead
in classChannelInboundHandlerAdapter
- Throws:
Exception
-
exceptionCaught
Description copied from class:ChannelInboundHandlerAdapter
CallsChannelHandlerContext.fireExceptionCaught(Throwable)
to forward to the nextChannelHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
exceptionCaught
in interfaceChannelHandler
- Specified by:
exceptionCaught
in interfaceChannelInboundHandler
- Overrides:
exceptionCaught
in classChannelInboundHandlerAdapter
- Throws:
Exception
-
disconnect
Description copied from class:ChannelDuplexHandler
CallsChannelOutboundInvoker.disconnect(ChannelPromise)
to forward to the nextChannelOutboundHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
disconnect
in interfaceChannelOutboundHandler
- Overrides:
disconnect
in classChannelDuplexHandler
- Parameters:
ctx
- theChannelHandlerContext
for which the disconnect operation is madepromise
- theChannelPromise
to notify once the operation completes- Throws:
Exception
- thrown if an error occurs
-
close
Description copied from class:ChannelDuplexHandler
CallsChannelOutboundInvoker.close(ChannelPromise)
to forward to the nextChannelOutboundHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
close
in interfaceChannelOutboundHandler
- Overrides:
close
in classChannelDuplexHandler
- Parameters:
ctx
- theChannelHandlerContext
for which the close operation is madepromise
- theChannelPromise
to notify once the operation completes- Throws:
Exception
- thrown if an error occurs
-
channelWritabilityChanged
Description copied from class:ChannelInboundHandlerAdapter
CallsChannelHandlerContext.fireChannelWritabilityChanged()
to forward to the nextChannelInboundHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
channelWritabilityChanged
in interfaceChannelInboundHandler
- Overrides:
channelWritabilityChanged
in classChannelInboundHandlerAdapter
- Throws:
Exception
-
handlerRemoved
Description copied from class:ChannelHandlerAdapter
Do nothing by default, sub-classes may override this method.- Specified by:
handlerRemoved
in interfaceChannelHandler
- Overrides:
handlerRemoved
in classChannelHandlerAdapter
- Throws:
Exception
-
resetReadAndFlushIfNeeded
-
flushIfNeeded
-
flushNow
-
scheduleFlush
-
cancelScheduledFlush
private void cancelScheduledFlush()
-