Home / Class/ FlowControlHandler Class — netty Architecture

FlowControlHandler Class — netty Architecture

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

Entity Profile

Dependency Diagram

graph TD
  a378d3b4_7977_a8fc_c1dd_b695ec779172["FlowControlHandler"]
  516f9009_28c1_b2fe_c361_f7bffd15e522["FlowControlHandler.java"]
  a378d3b4_7977_a8fc_c1dd_b695ec779172 -->|defined in| 516f9009_28c1_b2fe_c361_f7bffd15e522
  2108769c_a38c_306e_82ef_af0c8c05d06b["FlowControlHandler()"]
  a378d3b4_7977_a8fc_c1dd_b695ec779172 -->|method| 2108769c_a38c_306e_82ef_af0c8c05d06b
  ece3f16c_f517_592d_83b3_6b0a7b34835b["isQueueEmpty()"]
  a378d3b4_7977_a8fc_c1dd_b695ec779172 -->|method| ece3f16c_f517_592d_83b3_6b0a7b34835b
  dac1bd54_5aee_94e0_3f42_ccbaa3e43f7b["destroy()"]
  a378d3b4_7977_a8fc_c1dd_b695ec779172 -->|method| dac1bd54_5aee_94e0_3f42_ccbaa3e43f7b
  27415fdb_d5f6_1c03_a8ef_81342f8fe684["handlerAdded()"]
  a378d3b4_7977_a8fc_c1dd_b695ec779172 -->|method| 27415fdb_d5f6_1c03_a8ef_81342f8fe684
  cd6f0428_f8e3_c46a_35a0_7f323a7ab542["handlerRemoved()"]
  a378d3b4_7977_a8fc_c1dd_b695ec779172 -->|method| cd6f0428_f8e3_c46a_35a0_7f323a7ab542
  42db94c1_af12_1f19_80ee_c95c854f9085["channelInactive()"]
  a378d3b4_7977_a8fc_c1dd_b695ec779172 -->|method| 42db94c1_af12_1f19_80ee_c95c854f9085
  f4d16c7c_e26b_f445_c529_6abbac58ed56["read()"]
  a378d3b4_7977_a8fc_c1dd_b695ec779172 -->|method| f4d16c7c_e26b_f445_c529_6abbac58ed56
  ab1fcb76_21ea_cee4_50e0_0d3247fda36a["channelRead()"]
  a378d3b4_7977_a8fc_c1dd_b695ec779172 -->|method| ab1fcb76_21ea_cee4_50e0_0d3247fda36a
  7521bde4_9626_8e3b_0f5d_4bde6e9ffb9d["channelReadComplete()"]
  a378d3b4_7977_a8fc_c1dd_b695ec779172 -->|method| 7521bde4_9626_8e3b_0f5d_4bde6e9ffb9d
  231cc677_f63d_f911_377d_62f5edbedf36["dequeue()"]
  a378d3b4_7977_a8fc_c1dd_b695ec779172 -->|method| 231cc677_f63d_f911_377d_62f5edbedf36

Relationship Graph

Source Code

handler/src/main/java/io/netty/handler/flow/FlowControlHandler.java lines 67–255

public class FlowControlHandler extends ChannelDuplexHandler {
    private static final InternalLogger logger = InternalLoggerFactory.getInstance(FlowControlHandler.class);

    private final boolean releaseMessages;

    private RecyclableArrayDeque queue;

    private ChannelConfig config;

    private boolean shouldConsume;

    public FlowControlHandler() {
        this(true);
    }

    public FlowControlHandler(boolean releaseMessages) {
        this.releaseMessages = releaseMessages;
    }

    /**
     * Determine if the underlying {@link Queue} is empty. This method exists for
     * testing, debugging and inspection purposes and it is not Thread safe!
     */
    boolean isQueueEmpty() {
        return queue == null || queue.isEmpty();
    }

    /**
     * Releases all messages and destroys the {@link Queue}.
     */
    private void destroy() {
        if (queue != null) {

            if (!queue.isEmpty()) {
                logger.trace("Non-empty queue: {}", queue);

                if (releaseMessages) {
                    Object msg;
                    while ((msg = queue.poll()) != null) {
                        ReferenceCountUtil.safeRelease(msg);
                    }
                }
            }

            queue.recycle();
            queue = null;
        }
    }

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

    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
        super.handlerRemoved(ctx);
        if (!isQueueEmpty()) {
            dequeue(ctx, queue.size());
        }
        destroy();
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        destroy();
        ctx.fireChannelInactive();
    }

    @Override
    public void read(ChannelHandlerContext ctx) throws Exception {
        if (dequeue(ctx, 1) == 0) {
            // It seems no messages were consumed. We need to read() some
            // messages from upstream and once one arrives it need to be
            // relayed to downstream to keep the flow going.
            shouldConsume = true;
            ctx.read();
        } else if (config.isAutoRead()) {
            ctx.read();
        }
    }

Frequently Asked Questions

What is the FlowControlHandler class?
FlowControlHandler is a class in the netty codebase, defined in handler/src/main/java/io/netty/handler/flow/FlowControlHandler.java.
Where is FlowControlHandler defined?
FlowControlHandler is defined in handler/src/main/java/io/netty/handler/flow/FlowControlHandler.java at line 67.

Analyze Your Own Codebase

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

Try Supermodel Free