Top Description Fields Constructors Methods
io.netty5.handler.flush

public Class FlushConsolidationHandler

extends Object
implements ChannelHandler
Class Inheritance
All Implemented Interfaces
io.netty5.channel.ChannelHandler
Imports
io.netty5.channel.Channel, .ChannelHandler, .ChannelHandlerContext, .ChannelOutboundInvoker, .ChannelPipeline, io.netty5.util.concurrent.Future, io.netty5.util.internal.ObjectUtil

ChannelHandler which consolidates Channel#flush() / ChannelHandlerContext#flush() operations (which also includes Channel#writeAndFlush(Object) and ChannelOutboundInvoker#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 next ChannelHandler 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 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.

Field Summary

Modifier and TypeField and Description
private final boolean
private ChannelHandlerContext
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).

private final int
private int
private final Runnable
private Future<?>
private boolean

Constructor Summary

AccessConstructor and Description
public
FlushConsolidationHandler()

Create new instance which explicit flush after 256 pending flush operations at the latest.

public
FlushConsolidationHandler(int
the number of flushes after which an explicit flush will be done.
explicitFlushAfterFlushes
)

Create new instance which doesn't consolidate flushes when no read is in progress.

public
FlushConsolidationHandler(int
the number of flushes after which an explicit flush will be done.
explicitFlushAfterFlushes
,
boolean
whether to consolidate flushes even when no read loop is currently ongoing.
consolidateWhenNoReadInProgress
)

Create new instance.

Method Summary

Modifier and TypeMethod and Description
private void
public void
channelExceptionCaught(ChannelHandlerContext ctx, Throwable cause)

Overrides default io.netty5.channel.ChannelHandler.channelExceptionCaught.

Gets called if a Throwable was thrown when handling inbound events.
public void
channelRead(ChannelHandlerContext ctx, Object msg)

Overrides default io.netty5.channel.ChannelHandler.channelRead.

Invoked when the current Channel has read a message from the peer.
public void
channelReadComplete(ChannelHandlerContext ctx)

Overrides default io.netty5.channel.ChannelHandler.channelReadComplete.

Invoked when the last message read by the current read operation has been consumed by channelRead(ChannelHandlerContext, Object).
public void
channelWritabilityChanged(ChannelHandlerContext ctx)

Overrides default io.netty5.channel.ChannelHandler.channelWritabilityChanged.

Gets called once the writable state of a Channel changed.
public Future<Void>
close(ChannelHandlerContext
the ChannelHandlerContext for which the close operation is made
ctx
)

Overrides default io.netty5.channel.ChannelHandler.close.

Called once a close operation is made.
public Future<Void>
disconnect(ChannelHandlerContext
the ChannelHandlerContext for which the disconnect operation is made
ctx
)

Overrides default io.netty5.channel.ChannelHandler.disconnect.

Called once a disconnect operation is made.
public void
flush(ChannelHandlerContext
the ChannelHandlerContext for which the flush operation is made
ctx
)

Overrides default io.netty5.channel.ChannelHandler.flush.

Called once a flush operation is made.
private void
private void
public void
handlerAdded(ChannelHandlerContext ctx)

Overrides default io.netty5.channel.ChannelHandler.handlerAdded.

Gets called after the ChannelHandler was added to the actual context and it's ready to handle events.
public void
handlerRemoved(ChannelHandlerContext ctx)

Overrides default io.netty5.channel.ChannelHandler.handlerRemoved.

Gets called after the ChannelHandler was removed from the actual context and it doesn't handle events anymore.
private void
private void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

consolidateWhenNoReadInProgressback to summary
private final boolean consolidateWhenNoReadInProgress
ctxback to summary
private ChannelHandlerContext ctx
DEFAULT_EXPLICIT_FLUSH_AFTER_FLUSHESback to summary
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).

explicitFlushAfterFlushesback to summary
private final int explicitFlushAfterFlushes
flushPendingCountback to summary
private int flushPendingCount
flushTaskback to summary
private final Runnable flushTask
nextScheduledFlushback to summary
private Future<?> nextScheduledFlush
readInProgressback to summary
private boolean readInProgress

Constructor Detail

FlushConsolidationHandlerback to summary
public FlushConsolidationHandler()

Create new instance which explicit flush after 256 pending flush operations at the latest.

FlushConsolidationHandlerback to summary
public FlushConsolidationHandler(int explicitFlushAfterFlushes)

Create new instance which doesn't consolidate flushes when no read is in progress.

Parameters
explicitFlushAfterFlushes:int

the number of flushes after which an explicit flush will be done.

FlushConsolidationHandlerback to summary
public FlushConsolidationHandler(int explicitFlushAfterFlushes, boolean consolidateWhenNoReadInProgress)

Create new instance.

Parameters
explicitFlushAfterFlushes:int

the number of flushes after which an explicit flush will be done.

consolidateWhenNoReadInProgress:boolean

whether to consolidate flushes even when no read loop is currently ongoing.

Method Detail

cancelScheduledFlushback to summary
private void cancelScheduledFlush()
channelExceptionCaughtback to summary
public void channelExceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception

Overrides default io.netty5.channel.ChannelHandler.channelExceptionCaught.

Doc from io.netty5.channel.ChannelHandler.channelExceptionCaught.

Gets called if a Throwable was thrown when handling inbound events.

Annotations
@Override
channelReadback to summary
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception

Overrides default io.netty5.channel.ChannelHandler.channelRead.

Doc from io.netty5.channel.ChannelHandler.channelRead.

Invoked when the current Channel has read a message from the peer.

Annotations
@Override
channelReadCompleteback to summary
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception

Overrides default io.netty5.channel.ChannelHandler.channelReadComplete.

Doc from io.netty5.channel.ChannelHandler.channelReadComplete.

Invoked when the last message read by the current read operation has been consumed by channelRead(ChannelHandlerContext, Object). If ChannelOption#AUTO_READ is off, no further attempt to read an inbound data from the current Channel will be made until ChannelOutboundInvoker#read(ReadBufferAllocator) is called.

Annotations
@Override
channelWritabilityChangedback to summary
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception

Overrides default io.netty5.channel.ChannelHandler.channelWritabilityChanged.

Doc from io.netty5.channel.ChannelHandler.channelWritabilityChanged.

Gets called once the writable state of a Channel changed. You can check the state with Channel#writableBytes() or Channel#isWritable().

Annotations
@Override
closeback to summary
public Future<Void> close(ChannelHandlerContext ctx)

Overrides default io.netty5.channel.ChannelHandler.close.

Doc from io.netty5.channel.ChannelHandler.close.

Called once a close operation is made.

Parameters
ctx:ChannelHandlerContext

the ChannelHandlerContext for which the close operation is made

Returns:Future<Void>

the Future which will be notified once the operation completes.

Annotations
@Override
disconnectback to summary
public Future<Void> disconnect(ChannelHandlerContext ctx)

Overrides default io.netty5.channel.ChannelHandler.disconnect.

Doc from io.netty5.channel.ChannelHandler.disconnect.

Called once a disconnect operation is made.

Parameters
ctx:ChannelHandlerContext

the ChannelHandlerContext for which the disconnect operation is made

Returns:Future<Void>

the Future which will be notified once the operation completes.

Annotations
@Override
flushback to summary
public void flush(ChannelHandlerContext ctx)

Overrides default io.netty5.channel.ChannelHandler.flush.

Doc from io.netty5.channel.ChannelHandler.flush.

Called once a flush operation is made. The flush operation will try to flush out all previous written messages that are pending.

Parameters
ctx:ChannelHandlerContext

the ChannelHandlerContext for which the flush operation is made

Annotations
@Override
flushIfNeededback to summary
private void flushIfNeeded(ChannelHandlerContext ctx)
flushNowback to summary
private void flushNow(ChannelHandlerContext ctx)
handlerAddedback to summary
public void handlerAdded(ChannelHandlerContext ctx) throws Exception

Overrides default io.netty5.channel.ChannelHandler.handlerAdded.

Doc from io.netty5.channel.ChannelHandler.handlerAdded.

Gets called after the ChannelHandler was added to the actual context and it's ready to handle events.

Annotations
@Override
handlerRemovedback to summary
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception

Overrides default io.netty5.channel.ChannelHandler.handlerRemoved.

Doc from io.netty5.channel.ChannelHandler.handlerRemoved.

Gets called after the ChannelHandler was removed from the actual context and it doesn't handle events anymore.

Annotations
@Override
resetReadAndFlushIfNeededback to summary
private void resetReadAndFlushIfNeeded(ChannelHandlerContext ctx)
scheduleFlushback to summary
private void scheduleFlush(final ChannelHandlerContext ctx)