- java.lang.Object
-
- io.netty5.handler.flush.FlushConsolidationHandler
-
- All Implemented Interfaces:
ChannelHandler
public class FlushConsolidationHandler extends Object implements ChannelHandler
ChannelHandler
which consolidatesChannel.flush()
/ChannelHandlerContext.flush()
operations (which also includesChannel.writeAndFlush(Object)
andChannelOutboundInvoker.writeAndFlush(Object)
.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,
ChannelHandler.flush(ChannelHandlerContext)
will not be passed on to the nextChannelHandler
in theChannelPipeline
, as it will pick up any pending flushes whenchannelReadComplete(ChannelHandlerContext)
is triggered. If no read loop is ongoing, the behavior depends on theconsolidateWhenNoReadInProgress
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 firstChannelHandler
in theChannelPipeline
to have the best effect.
-
-
Field Summary
Fields Modifier and Type Field Description static 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).
-
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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
channelExceptionCaught(ChannelHandlerContext ctx, Throwable cause)
Gets called if aThrowable
was thrown when handling inbound events.void
channelRead(ChannelHandlerContext ctx, Object msg)
Invoked when the currentChannel
has read a message from the peer.void
channelReadComplete(ChannelHandlerContext ctx)
Invoked when the last message read by the current read operation has been consumed byChannelHandler.channelRead(ChannelHandlerContext, Object)
.void
channelWritabilityChanged(ChannelHandlerContext ctx)
Gets called once the writable state of aChannel
changed.Future<Void>
close(ChannelHandlerContext ctx)
Called once a close operation is made.Future<Void>
disconnect(ChannelHandlerContext ctx)
Called once a disconnect operation is made.void
flush(ChannelHandlerContext ctx)
Called once a flush operation is made.void
handlerAdded(ChannelHandlerContext ctx)
Gets called after theChannelHandler
was added to the actual context and it's ready to handle events.void
handlerRemoved(ChannelHandlerContext ctx)
Gets called after theChannelHandler
was removed from the actual context and it doesn't handle events anymore.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.netty5.channel.ChannelHandler
bind, channelActive, channelInactive, channelInboundEvent, channelRegistered, channelShutdown, channelUnregistered, connect, deregister, isSharable, pendingOutboundBytes, read, register, sendOutboundEvent, shutdown, write
-
-
-
-
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 Exception
Description copied from interface:ChannelHandler
Gets called after theChannelHandler
was added to the actual context and it's ready to handle events.- Specified by:
handlerAdded
in interfaceChannelHandler
- Throws:
Exception
-
flush
public void flush(ChannelHandlerContext ctx)
Description copied from interface:ChannelHandler
Called once a flush operation is made. The flush operation will try to flush out all previous written messages that are pending.- Specified by:
flush
in interfaceChannelHandler
- Parameters:
ctx
- theChannelHandlerContext
for which the flush operation is made
-
channelReadComplete
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception
Description copied from interface:ChannelHandler
Invoked when the last message read by the current read operation has been consumed byChannelHandler.channelRead(ChannelHandlerContext, Object)
. IfChannelOption.AUTO_READ
is off, no further attempt to read an inbound data from the currentChannel
will be made untilChannelHandlerContext.read()
is called.- Specified by:
channelReadComplete
in interfaceChannelHandler
- Throws:
Exception
-
channelRead
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
Description copied from interface:ChannelHandler
Invoked when the currentChannel
has read a message from the peer.- Specified by:
channelRead
in interfaceChannelHandler
- Throws:
Exception
-
channelExceptionCaught
public void channelExceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception
Description copied from interface:ChannelHandler
Gets called if aThrowable
was thrown when handling inbound events.- Specified by:
channelExceptionCaught
in interfaceChannelHandler
- Throws:
Exception
-
disconnect
public Future<Void> disconnect(ChannelHandlerContext ctx)
Description copied from interface:ChannelHandler
Called once a disconnect operation is made.- Specified by:
disconnect
in interfaceChannelHandler
- Parameters:
ctx
- theChannelHandlerContext
for which the disconnect operation is made- Returns:
- the
Future
which will be notified once the operation completes.
-
close
public Future<Void> close(ChannelHandlerContext ctx)
Description copied from interface:ChannelHandler
Called once a close operation is made.- Specified by:
close
in interfaceChannelHandler
- Parameters:
ctx
- theChannelHandlerContext
for which the close operation is made- Returns:
- the
Future
which will be notified once the operation completes.
-
channelWritabilityChanged
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception
Description copied from interface:ChannelHandler
Gets called once the writable state of aChannel
changed. You can check the state withChannel.writableBytes()
orChannel.isWritable()
.- Specified by:
channelWritabilityChanged
in interfaceChannelHandler
- Throws:
Exception
-
handlerRemoved
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception
Description copied from interface:ChannelHandler
Gets called after theChannelHandler
was removed from the actual context and it doesn't handle events anymore.- Specified by:
handlerRemoved
in interfaceChannelHandler
- Throws:
Exception
-
-