Home / Function/ select() — netty Function Reference

select() — netty Function Reference

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

Function java Buffer Telemetry calls 2 called by 3

Entity Profile

Dependency Diagram

graph TD
  b18c5ab3_f5bb_81ad_70bd_20524d394f59["select()"]
  db526e28_8aae_a182_b56b_dc12824d89f5["NioIoHandler"]
  b18c5ab3_f5bb_81ad_70bd_20524d394f59 -->|defined in| db526e28_8aae_a182_b56b_dc12824d89f5
  60fdcec8_2616_1645_9189_9d99251306f1["IoRegistration()"]
  60fdcec8_2616_1645_9189_9d99251306f1 -->|calls| b18c5ab3_f5bb_81ad_70bd_20524d394f59
  bfebd9e6_05a3_0eea_3b0f_b34d5870ab36["run()"]
  bfebd9e6_05a3_0eea_3b0f_b34d5870ab36 -->|calls| b18c5ab3_f5bb_81ad_70bd_20524d394f59
  91e9bee4_1b62_e088_9fb3_4b12427baefc["Selector()"]
  91e9bee4_1b62_e088_9fb3_4b12427baefc -->|calls| b18c5ab3_f5bb_81ad_70bd_20524d394f59
  430fa9ce_cc53_2a86_0feb_11916b32c02f["millisBeforeDeadline()"]
  b18c5ab3_f5bb_81ad_70bd_20524d394f59 -->|calls| 430fa9ce_cc53_2a86_0feb_11916b32c02f
  5c2d7172_e563_2902_636d_504657d6c8d2["selectNow()"]
  b18c5ab3_f5bb_81ad_70bd_20524d394f59 -->|calls| 5c2d7172_e563_2902_636d_504657d6c8d2
  style b18c5ab3_f5bb_81ad_70bd_20524d394f59 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

transport/src/main/java/io/netty/channel/nio/NioIoHandler.java lines 630–722

    private void select(IoHandlerContext runner, boolean oldWakenUp) throws IOException {
        Selector selector = this.selector;
        try {
            int selectCnt = 0;
            long currentTimeNanos = System.nanoTime();
            final long delayNanos = runner.delayNanos(currentTimeNanos);
            // that's some special value which is used to indicate that no scheduled task is present.
            // we set the deadline to a bogus (unused) value for us to represent infinity
            long selectDeadLineNanos = Long.MAX_VALUE;
            if (delayNanos != Long.MAX_VALUE) {
                selectDeadLineNanos = currentTimeNanos + runner.delayNanos(currentTimeNanos);
            }
            for (;;) {
                final long timeoutMillis;
                if (delayNanos != Long.MAX_VALUE) {
                    long millisBeforeDeadline = millisBeforeDeadline(selectDeadLineNanos, currentTimeNanos);
                    if (millisBeforeDeadline <= 0) {
                        if (selectCnt == 0) {
                            selector.selectNow();
                            selectCnt = 1;
                        }
                        break;
                    }
                    timeoutMillis = millisBeforeDeadline;
                } else {
                    // in NIO this means to block without any deadline
                    timeoutMillis = 0;
                }
                // If a task was submitted when wakenUp value was true, the task didn't get a chance to call
                // Selector#wakeup. So we need to check task queue again before executing select operation.
                // If we don't, the task might be pended until select operation was timed out.
                // It might be pended until idle timeout if IdleStateHandler existed in pipeline.
                if (!runner.canBlock() && wakenUp.compareAndSet(false, true)) {
                    selector.selectNow();
                    selectCnt = 1;
                    break;
                }

                int selectedKeys = selector.select(timeoutMillis);
                selectCnt ++;

                if (selectedKeys != 0 || oldWakenUp || wakenUp.get() || !runner.canBlock()) {
                    // - Selected something,
                    // - waken up by user, or
                    // - the task queue has a pending task.
                    // - a scheduled task is ready for processing
                    break;
                }
                if (Thread.interrupted()) {
                    // Thread was interrupted so reset selected keys and break so we not run into a busy loop.
                    // As this is most likely a bug in the handler of the user or it's client library we will
                    // also log it.
                    //
                    // See https://github.com/netty/netty/issues/2426
                    if (logger.isDebugEnabled()) {
                        logger.debug("Selector.select() returned prematurely because " +
                                "Thread.currentThread().interrupt() was called. Use " +
                                "NioHandler.shutdownGracefully() to shutdown the NioHandler.");
                    }
                    selectCnt = 1;
                    break;
                }

                long time = System.nanoTime();
                if (time - TimeUnit.MILLISECONDS.toNanos(timeoutMillis) >= currentTimeNanos) {
                    // timeoutMillis elapsed without anything selected.
                    selectCnt = 1;
                } else if (SELECTOR_AUTO_REBUILD_THRESHOLD > 0 &&
                        selectCnt >= SELECTOR_AUTO_REBUILD_THRESHOLD) {
                    // The code exists in an extra method to ensure the method is not too big to inline as this
                    // branch is not very likely to get hit very frequently.
                    selector = selectRebuildSelector(selectCnt);
                    selectCnt = 1;
                    break;
                }

                currentTimeNanos = time;
            }

            if (selectCnt > MIN_PREMATURE_SELECTOR_RETURNS) {
                if (logger.isDebugEnabled()) {

Domain

Subdomains

Frequently Asked Questions

What does select() do?
select() is a function in the netty codebase, defined in transport/src/main/java/io/netty/channel/nio/NioIoHandler.java.
Where is select() defined?
select() is defined in transport/src/main/java/io/netty/channel/nio/NioIoHandler.java at line 630.
What does select() call?
select() calls 2 function(s): millisBeforeDeadline, selectNow.
What calls select()?
select() is called by 3 function(s): IoRegistration, Selector, run.

Analyze Your Own Codebase

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

Try Supermodel Free