ChannelDuplexHandler which consolidates Channel#flush() / ChannelHandlerContext#flush()
operations (which also includes
Channel#writeAndFlush(Object) / Channel#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:
false, flushes are passed on to the next handler directly;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.
| Modifier and Type | Field 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 |
| Access | Constructor 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. |
| consolidateWhenNoReadInProgress | back to summary |
|---|---|
| private final boolean consolidateWhenNoReadInProgress | |
| ctx | back to summary |
|---|---|
| private ChannelHandlerContext ctx | |
| DEFAULT_EXPLICIT_FLUSH_AFTER_FLUSHES | back 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). | |
| explicitFlushAfterFlushes | back to summary |
|---|---|
| private final int explicitFlushAfterFlushes | |
| flushPendingCount | back to summary |
|---|---|
| private int flushPendingCount | |
| flushTask | back to summary |
|---|---|
| private final Runnable flushTask | |
| nextScheduledFlush | back to summary |
|---|---|
| private Future<?> nextScheduledFlush | |
| readInProgress | back to summary |
|---|---|
| private boolean readInProgress | |
| FlushConsolidationHandler | back to summary |
|---|---|
| public FlushConsolidationHandler() Create new instance which explicit flush after 256 pending flush operations at the latest. | |
| FlushConsolidationHandler | back to summary |
|---|---|
| public FlushConsolidationHandler(int explicitFlushAfterFlushes) Create new instance which doesn't consolidate flushes when no read is in progress.
| |
| FlushConsolidationHandler | back to summary |
|---|---|
| public FlushConsolidationHandler(int explicitFlushAfterFlushes, boolean consolidateWhenNoReadInProgress) Create new instance.
| |
| cancelScheduledFlush | back to summary |
|---|---|
| private void cancelScheduledFlush() | |
| channelRead | back to summary |
|---|---|
| public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception Overrides io. Implements io. Doc from io. Calls
| |
| channelReadComplete | back to summary |
|---|---|
| public void channelReadComplete(ChannelHandlerContext ctx) throws Exception Overrides io. Implements io. Doc from io. Calls
| |
| channelWritabilityChanged | back to summary |
|---|---|
| public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception Overrides io. Implements io. Doc from io. Calls
| |
| close | back to summary |
|---|---|
| public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception Overrides io. Implements io. Doc from io. Calls
| |
| disconnect | back to summary |
|---|---|
| public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception Overrides io. Implements io. Doc from io. Calls
| |
| exceptionCaught | back to summary |
|---|---|
| public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception Overrides io. Implements io. Doc from io. Gets called if a
| |
| flush | back to summary |
|---|---|
| public void flush(ChannelHandlerContext ctx) throws Exception Overrides io. Implements io. Doc from io. Calls
| |
| flushIfNeeded | back to summary |
|---|---|
| private void flushIfNeeded(ChannelHandlerContext ctx) | |
| flushNow | back to summary |
|---|---|
| private void flushNow(ChannelHandlerContext ctx) | |
| handlerAdded | back to summary |
|---|---|
| public void handlerAdded(ChannelHandlerContext ctx) throws Exception Overrides io. Implements io. Doc from io. Gets called after the
| |
| handlerRemoved | back to summary |
|---|---|
| public void handlerRemoved(ChannelHandlerContext ctx) throws Exception Overrides io. Implements io. Doc from io. Gets called after the
| |
| resetReadAndFlushIfNeeded | back to summary |
|---|---|
| private void resetReadAndFlushIfNeeded(ChannelHandlerContext ctx) | |
| scheduleFlush | back to summary |
|---|---|
| private void scheduleFlush(final ChannelHandlerContext ctx) | |