Class NioIoHandler

java.lang.Object
io.netty.channel.nio.NioIoHandler
All Implemented Interfaces:
IoHandler

public final class NioIoHandler extends java.lang.Object implements IoHandler
IoHandler implementation which register the IoHandle's to a Selector.
  • Field Details

    • logger

      private static final InternalLogger logger
    • CLEANUP_INTERVAL

      private static final int CLEANUP_INTERVAL
      See Also:
    • DISABLE_KEY_SET_OPTIMIZATION

      private static final boolean DISABLE_KEY_SET_OPTIMIZATION
    • MIN_PREMATURE_SELECTOR_RETURNS

      private static final int MIN_PREMATURE_SELECTOR_RETURNS
      See Also:
    • SELECTOR_AUTO_REBUILD_THRESHOLD

      private static final int SELECTOR_AUTO_REBUILD_THRESHOLD
    • selectNowSupplier

      private final IntSupplier selectNowSupplier
    • selector

      private java.nio.channels.Selector selector
      The NIO Selector.
    • unwrappedSelector

      private java.nio.channels.Selector unwrappedSelector
    • selectedKeys

      private SelectedSelectionKeySet selectedKeys
    • provider

      private final java.nio.channels.spi.SelectorProvider provider
    • wakenUp

      private final java.util.concurrent.atomic.AtomicBoolean wakenUp
      Boolean that controls determines if a blocked Selector.select should break out of its selection process. In our case we use a timeout for the select method and the select method will block for that time unless waken up.
    • selectStrategy

      private final SelectStrategy selectStrategy
    • executor

      private final ThreadAwareExecutor executor
    • cancelledKeys

      private int cancelledKeys
    • needsToSelectAgain

      private boolean needsToSelectAgain
  • Constructor Details

  • Method Details

    • openSelector

      private NioIoHandler.SelectorTuple openSelector()
    • selectorProvider

      public java.nio.channels.spi.SelectorProvider selectorProvider()
      Returns the SelectorProvider used by this NioEventLoop to obtain the Selector.
    • selector

      java.nio.channels.Selector selector()
    • numRegistered

      int numRegistered()
    • registeredSet

      java.util.Set<java.nio.channels.SelectionKey> registeredSet()
    • rebuildSelector0

      void rebuildSelector0()
    • nioHandle

      private static NioIoHandle nioHandle(IoHandle handle)
    • cast

      private static NioIoOps cast(IoOps ops)
    • register

      public IoRegistration register(IoHandle handle) throws java.lang.Exception
      Description copied from interface: IoHandler
      Register a IoHandle for IO.
      Specified by:
      register in interface IoHandler
      Parameters:
      handle - the IoHandle to register.
      Throws:
      java.lang.Exception - thrown if an error happens during registration.
    • run

      public int run(IoHandlerContext context)
      Description copied from interface: IoHandler
      Run the IO handled by this IoHandler. The IoHandlerContext should be used to ensure we not execute too long and so block the processing of other task that are scheduled on the ThreadAwareExecutor. This is done by taking IoHandlerContext.delayNanos(long) or IoHandlerContext.deadlineNanos() into account.
      Specified by:
      run in interface IoHandler
      Parameters:
      context - the IoHandlerContext.
      Returns:
      the number of IoHandle for which I/O was handled. Internal events such as wakeups and timer expirations must not be included in this count.
    • handleLoopException

      private static void handleLoopException(java.lang.Throwable t)
    • processSelectedKeys

      private int processSelectedKeys()
    • destroy

      public void destroy()
      Description copied from interface: IoHandler
      Destroy the IoHandler and free all its resources. Once destroyed using the IoHandler will cause undefined behaviour.
      Specified by:
      destroy in interface IoHandler
    • processSelectedKeysPlain

      private int processSelectedKeysPlain(java.util.Set<java.nio.channels.SelectionKey> selectedKeys)
    • processSelectedKeysOptimized

      private int processSelectedKeysOptimized()
    • processSelectedKey

      private void processSelectedKey(java.nio.channels.SelectionKey k)
    • prepareToDestroy

      public void prepareToDestroy()
      Description copied from interface: IoHandler
      Prepare to destroy this IoHandler. This method will be called before IoHandler.destroy() and may be called multiple times.
      Specified by:
      prepareToDestroy in interface IoHandler
    • wakeup

      public void wakeup()
      Description copied from interface: IoHandler
      Wakeup the IoHandler, which means if any operation blocks it should be unblocked and return as soon as possible.
      Specified by:
      wakeup in interface IoHandler
    • isCompatible

      public boolean isCompatible(java.lang.Class<? extends IoHandle> handleType)
      Description copied from interface: IoHandler
      Returns true if the given type is compatible with this IoHandler and so can be registered, false otherwise.
      Specified by:
      isCompatible in interface IoHandler
      Parameters:
      handleType - the type of the IoHandle.
      Returns:
      if compatible of not.
    • unwrappedSelector

      java.nio.channels.Selector unwrappedSelector()
    • select

      private void select(IoHandlerContext runner, boolean oldWakenUp) throws java.io.IOException
      Throws:
      java.io.IOException
    • millisBeforeDeadline

      static long millisBeforeDeadline(long selectDeadLineNanos, long currentTimeNanos)
      Returns the timeout in milliseconds for Selector.select(long) when the next scheduled task is due after the given deadline. The value is rounded up, so it is only 0 if the deadline is less than 5 microseconds away. Rounding to the nearest millisecond would return 0 for a deadline less than 0.5 ms away, and the event loop would then call Selector.selectNow() in a loop, burning CPU, until the deadline is reached.
    • selectNow

      int selectNow() throws java.io.IOException
      Throws:
      java.io.IOException
    • selectRebuildSelector

      private java.nio.channels.Selector selectRebuildSelector(int selectCnt) throws java.io.IOException
      Throws:
      java.io.IOException
    • selectAgain

      private void selectAgain()
    • newFactory

      public static IoHandlerFactory newFactory()
      Returns a new IoHandlerFactory that creates NioIoHandler instances
      Returns:
      factory the IoHandlerFactory.
    • newFactory

      public static IoHandlerFactory newFactory(java.nio.channels.spi.SelectorProvider selectorProvider)
      Returns a new IoHandlerFactory that creates NioIoHandler instances.
      Parameters:
      selectorProvider - the SelectorProvider to use.
      Returns:
      factory the IoHandlerFactory.
    • newFactory

      public static IoHandlerFactory newFactory(java.nio.channels.spi.SelectorProvider selectorProvider, SelectStrategyFactory selectStrategyFactory)
      Returns a new IoHandlerFactory that creates NioIoHandler instances.
      Parameters:
      selectorProvider - the SelectorProvider to use.
      selectStrategyFactory - the SelectStrategyFactory to use.
      Returns:
      factory the IoHandlerFactory.