Home / Function/ run() — netty Function Reference

run() — netty Function Reference

Architecture documentation for the run() function in NioIoHandler.java from the netty codebase.

Function java Buffer Telemetry calls 5 called by 1

Entity Profile

Dependency Diagram

graph TD
  bfebd9e6_05a3_0eea_3b0f_b34d5870ab36["run()"]
  db526e28_8aae_a182_b56b_dc12824d89f5["NioIoHandler"]
  bfebd9e6_05a3_0eea_3b0f_b34d5870ab36 -->|defined in| db526e28_8aae_a182_b56b_dc12824d89f5
  d43dc98f_6738_d8a6_e838_daf73bfcfdca["SelectorTuple()"]
  d43dc98f_6738_d8a6_e838_daf73bfcfdca -->|calls| bfebd9e6_05a3_0eea_3b0f_b34d5870ab36
  b18c5ab3_f5bb_81ad_70bd_20524d394f59["select()"]
  bfebd9e6_05a3_0eea_3b0f_b34d5870ab36 -->|calls| b18c5ab3_f5bb_81ad_70bd_20524d394f59
  b23ac8e4_5eac_4595_0470_59e0db8713ff["wakeup()"]
  bfebd9e6_05a3_0eea_3b0f_b34d5870ab36 -->|calls| b23ac8e4_5eac_4595_0470_59e0db8713ff
  0cde094c_1fde_4ca0_aa21_b607ebeae5b5["rebuildSelector0()"]
  bfebd9e6_05a3_0eea_3b0f_b34d5870ab36 -->|calls| 0cde094c_1fde_4ca0_aa21_b607ebeae5b5
  7006124b_c1a3_1e03_afbd_e97032ecd2dd["handleLoopException()"]
  bfebd9e6_05a3_0eea_3b0f_b34d5870ab36 -->|calls| 7006124b_c1a3_1e03_afbd_e97032ecd2dd
  bb59a7a2_77ef_52ff_0c57_820c55b1ec5b["processSelectedKeys()"]
  bfebd9e6_05a3_0eea_3b0f_b34d5870ab36 -->|calls| bb59a7a2_77ef_52ff_0c57_820c55b1ec5b
  style bfebd9e6_05a3_0eea_3b0f_b34d5870ab36 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

transport/src/main/java/io/netty/channel/nio/NioIoHandler.java lines 418–496

    @Override
    public int run(IoHandlerContext context) {
        int handled = 0;
        try {
            try {
                switch (selectStrategy.calculateStrategy(selectNowSupplier, !context.canBlock())) {
                    case SelectStrategy.CONTINUE:
                        if (context.shouldReportActiveIoTime()) {
                            context.reportActiveIoTime(0); // Report zero as we did no I/O.
                        }
                        return 0;

                    case SelectStrategy.BUSY_WAIT:
                        // fall-through to SELECT since the busy-wait is not supported with NIO

                    case SelectStrategy.SELECT:
                        select(context, wakenUp.getAndSet(false));

                        // 'wakenUp.compareAndSet(false, true)' is always evaluated
                        // before calling 'selector.wakeup()' to reduce the wake-up
                        // overhead. (Selector.wakeup() is an expensive operation.)
                        //
                        // However, there is a race condition in this approach.
                        // The race condition is triggered when 'wakenUp' is set to
                        // true too early.
                        //
                        // 'wakenUp' is set to true too early if:
                        // 1) Selector is waken up between 'wakenUp.set(false)' and
                        //    'selector.select(...)'. (BAD)
                        // 2) Selector is waken up between 'selector.select(...)' and
                        //    'if (wakenUp.get()) { ... }'. (OK)
                        //
                        // In the first case, 'wakenUp' is set to true and the
                        // following 'selector.select(...)' will wake up immediately.
                        // Until 'wakenUp' is set to false again in the next round,
                        // 'wakenUp.compareAndSet(false, true)' will fail, and therefore
                        // any attempt to wake up the Selector will fail, too, causing
                        // the following 'selector.select(...)' call to block
                        // unnecessarily.
                        //
                        // To fix this problem, we wake up the selector again if wakenUp
                        // is true immediately after selector.select(...).
                        // It is inefficient in that it wakes up the selector for both
                        // the first case (BAD - wake-up required) and the second case
                        // (OK - no wake-up required).

                        if (wakenUp.get()) {
                            selector.wakeup();
                        }
                        // fall through
                    default:
                }
            } catch (IOException e) {
                // If we receive an IOException here its because the Selector is messed up. Let's rebuild
                // the selector and retry. https://github.com/netty/netty/issues/8566
                rebuildSelector0();
                handleLoopException(e);
                return 0;
            }

            cancelledKeys = 0;
            needsToSelectAgain = false;

            if (context.shouldReportActiveIoTime()) {
                // We start the timer after the blocking select() call has returned.
                long activeIoStartTimeNanos = System.nanoTime();
                handled = processSelectedKeys();
                long activeIoEndTimeNanos = System.nanoTime();
                context.reportActiveIoTime(activeIoEndTimeNanos - activeIoStartTimeNanos);
            } else {
                handled = processSelectedKeys();
            }
        } catch (Error e) {
            throw e;
        } catch (Throwable t) {
            handleLoopException(t);
        }
        return handled;
    }

Domain

Subdomains

Called By

Frequently Asked Questions

What does run() do?
run() is a function in the netty codebase, defined in transport/src/main/java/io/netty/channel/nio/NioIoHandler.java.
Where is run() defined?
run() is defined in transport/src/main/java/io/netty/channel/nio/NioIoHandler.java at line 418.
What does run() call?
run() calls 5 function(s): handleLoopException, processSelectedKeys, rebuildSelector0, select, wakeup.
What calls run()?
run() is called by 1 function(s): SelectorTuple.

Analyze Your Own Codebase

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

Try Supermodel Free