Home / Class/ FlushConsolidationHandler Class — netty Architecture

FlushConsolidationHandler Class — netty Architecture

Architecture documentation for the FlushConsolidationHandler class in FlushConsolidationHandler.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  2c83d6ba_1bff_324b_5825_8c26ffd14953["FlushConsolidationHandler"]
  0e02c43a_5300_8a92_c7ba_22479d24aa28["FlushConsolidationHandler.java"]
  2c83d6ba_1bff_324b_5825_8c26ffd14953 -->|defined in| 0e02c43a_5300_8a92_c7ba_22479d24aa28
  8725031c_7bfb_d03f_d0f0_a4da7647d2b8["FlushConsolidationHandler()"]
  2c83d6ba_1bff_324b_5825_8c26ffd14953 -->|method| 8725031c_7bfb_d03f_d0f0_a4da7647d2b8
  3e4fc157_ca33_fdbb_564e_721987aae1bf["handlerAdded()"]
  2c83d6ba_1bff_324b_5825_8c26ffd14953 -->|method| 3e4fc157_ca33_fdbb_564e_721987aae1bf
  7fbc654e_1d7f_5b1f_c7ad_939c24a30413["flush()"]
  2c83d6ba_1bff_324b_5825_8c26ffd14953 -->|method| 7fbc654e_1d7f_5b1f_c7ad_939c24a30413
  84d37731_7d4c_dd16_40b5_a6526cf85dc7["channelReadComplete()"]
  2c83d6ba_1bff_324b_5825_8c26ffd14953 -->|method| 84d37731_7d4c_dd16_40b5_a6526cf85dc7
  faf0b6c3_a494_7d36_c1f8_212d8f9cd043["channelRead()"]
  2c83d6ba_1bff_324b_5825_8c26ffd14953 -->|method| faf0b6c3_a494_7d36_c1f8_212d8f9cd043
  7437c890_0d15_3a05_609a_8e5a5c093d16["exceptionCaught()"]
  2c83d6ba_1bff_324b_5825_8c26ffd14953 -->|method| 7437c890_0d15_3a05_609a_8e5a5c093d16
  c3eee857_e5d8_4e4b_d2dc_45e6f2b3e717["disconnect()"]
  2c83d6ba_1bff_324b_5825_8c26ffd14953 -->|method| c3eee857_e5d8_4e4b_d2dc_45e6f2b3e717
  4f031e04_448f_d394_a879_211daecaab6f["close()"]
  2c83d6ba_1bff_324b_5825_8c26ffd14953 -->|method| 4f031e04_448f_d394_a879_211daecaab6f
  0f2773f4_ad95_8be9_0511_a9d657a2f9dc["channelWritabilityChanged()"]
  2c83d6ba_1bff_324b_5825_8c26ffd14953 -->|method| 0f2773f4_ad95_8be9_0511_a9d657a2f9dc
  9ddbeddc_a138_bf5b_ac4d_bd6e47b387cf["handlerRemoved()"]
  2c83d6ba_1bff_324b_5825_8c26ffd14953 -->|method| 9ddbeddc_a138_bf5b_ac4d_bd6e47b387cf
  90a0a550_7b82_0f93_7dda_d451eafd5a3f["resetReadAndFlushIfNeeded()"]
  2c83d6ba_1bff_324b_5825_8c26ffd14953 -->|method| 90a0a550_7b82_0f93_7dda_d451eafd5a3f
  23231906_06a5_46d9_5278_bb246be7470c["flushIfNeeded()"]
  2c83d6ba_1bff_324b_5825_8c26ffd14953 -->|method| 23231906_06a5_46d9_5278_bb246be7470c
  b4f0e143_9831_d5e8_f170_2f33e1c026f7["flushNow()"]
  2c83d6ba_1bff_324b_5825_8c26ffd14953 -->|method| b4f0e143_9831_d5e8_f170_2f33e1c026f7

Relationship Graph

Source Code

handler/src/main/java/io/netty/handler/flush/FlushConsolidationHandler.java lines 59–220

public class FlushConsolidationHandler extends ChannelDuplexHandler {
    private final int explicitFlushAfterFlushes;
    private final boolean consolidateWhenNoReadInProgress;
    private final Runnable flushTask;
    private int flushPendingCount;
    private boolean readInProgress;
    private ChannelHandlerContext ctx;
    private Future<?> nextScheduledFlush;

    /**
     * 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).
     */
    public static final int DEFAULT_EXPLICIT_FLUSH_AFTER_FLUSHES = 256;

    /**
     * Create new instance which explicit flush after {@value DEFAULT_EXPLICIT_FLUSH_AFTER_FLUSHES} pending flush
     * operations at the latest.
     */
    public FlushConsolidationHandler() {
        this(DEFAULT_EXPLICIT_FLUSH_AFTER_FLUSHES, false);
    }

    /**
     * Create new instance which doesn't consolidate flushes when no read is in progress.
     *
     * @param explicitFlushAfterFlushes the number of flushes after which an explicit flush will be done.
     */
    public FlushConsolidationHandler(int explicitFlushAfterFlushes) {
        this(explicitFlushAfterFlushes, false);
    }

    /**
     * Create new instance.
     *
     * @param explicitFlushAfterFlushes the number of flushes after which an explicit flush will be done.
     * @param consolidateWhenNoReadInProgress whether to consolidate flushes even when no read loop is currently
     *                                        ongoing.
     */
    public FlushConsolidationHandler(int explicitFlushAfterFlushes, boolean consolidateWhenNoReadInProgress) {
        this.explicitFlushAfterFlushes =
                ObjectUtil.checkPositive(explicitFlushAfterFlushes, "explicitFlushAfterFlushes");
        this.consolidateWhenNoReadInProgress = consolidateWhenNoReadInProgress;
        this.flushTask = consolidateWhenNoReadInProgress ?
                new Runnable() {
                    @Override
                    public void run() {
                        if (flushPendingCount > 0 && !readInProgress) {
                            flushPendingCount = 0;
                            nextScheduledFlush = null;
                            ctx.flush();
                        } // else we'll flush when the read completes
                    }
                }
                : null;
    }

    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        this.ctx = ctx;
    }

    @Override
    public void flush(ChannelHandlerContext ctx) throws Exception {
        if (readInProgress) {
            // If there is still a read in progress we are sure we will see a channelReadComplete(...) call. Thus
            // we only need to flush if we reach the explicitFlushAfterFlushes limit.
            if (++flushPendingCount == explicitFlushAfterFlushes) {
                flushNow(ctx);
            }
        } else if (consolidateWhenNoReadInProgress) {
            // Flush immediately if we reach the threshold, otherwise schedule
            if (++flushPendingCount == explicitFlushAfterFlushes) {
                flushNow(ctx);
            } else {
                scheduleFlush(ctx);
            }
        } else {
            // Always flush directly
            flushNow(ctx);
        }

Frequently Asked Questions

What is the FlushConsolidationHandler class?
FlushConsolidationHandler is a class in the netty codebase, defined in handler/src/main/java/io/netty/handler/flush/FlushConsolidationHandler.java.
Where is FlushConsolidationHandler defined?
FlushConsolidationHandler is defined in handler/src/main/java/io/netty/handler/flush/FlushConsolidationHandler.java at line 59.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free