java.lang.Object
net.i2p.router.transport.ntcp.Reader

public class Readerextends Object
Pool of running threads which will process any read bytes on any of the NTCPConnections, including decryption of the data read, connection handshaking, parsing bytes into I2NP messages, etc. Provides efficient buffer management and connection lifecycle coordination. Supports dynamic thread count adjustment via adjustThreads().
  • Field Details

    • _log

      private final Log _log
      Logger for the NTCP reader.
    • _context

      private final RouterContext _context
      The router context.
    • _pendingConnections

      private final Set<NTCPConnection> _pendingConnections
      Connections waiting for a runner.
    • _liveReads

      private final Set<NTCPConnection> _liveReads
      Connections being read by a runner.
    • _readAfterLive

      private final Set<NTCPConnection> _readAfterLive
      Connections to read after the live ones.
    • _runners

      private final CopyOnWriteArrayList<Reader.Runner> _runners
      The running reader threads.
    • _threadCount

      private static volatile int _threadCount
      Number of reader threads.
    • MIN_THREADS

      private static final int MIN_THREADS
      Fewest reader threads allowed.
      See Also:
    • MAX_THREADS

      private static final int MAX_THREADS
      Most reader threads allowed.
      See Also:
    • _threadNum

      private static final AtomicInteger _threadNum
      Sequence number of the reader threads.
    • _activeCount

      private final AtomicInteger _activeCount
      Tracks how many runner threads are actively processing (not parked).
  • Constructor Details

    • Reader

      public Reader(RouterContext ctx)
      Reader thread pool for the given context.
      Parameters:
      ctx - the router context supplying the log and the clock
  • Method Details

    • getThreadCount

      public static int getThreadCount()
      Reader thread count.
      Returns:
      the target number of reader threads, 2 to 16
    • getActiveCount

      public int getActiveCount()
      Number of threads currently processing (not parked).
      Returns:
      the count of runners that are inside the read loop
    • getUtilization

      public double getUtilization()
      Reader pool utilization as a ratio (0.0-1.0). NaN if no runners are active (pool not started).
      Returns:
      the utilization as a ratio from 0.0 to 1.0, or NaN if no runners are active
    • setThreadCount

      public static void setThreadCount(int count)
      Reader thread count, bounded by MIN_THREADS-MAX_THREADS.
      Parameters:
      count - the requested target, clamped into the range 2 to 16
    • startReading

      public void startReading(int numReaders)
      Starts the given number of reader threads.
      Parameters:
      numReaders - how many runners to add to those already started
    • startRunner

      private void startRunner()
      Starts one more reader thread if needed.
    • adjustThreads

      public void adjustThreads()
      Dynamically adjust thread count to match the target.
    • stopReading

      public void stopReading()
      Stop all reader threads.
    • wantsRead

      public void wantsRead(NTCPConnection con)
      Registers a connection to be read. High-frequency per-buffer path.
      Parameters:
      con - the connection with data waiting to be read
    • connectionClosed

      public void connectionClosed(NTCPConnection con)
      Removes a closed connection from the queues.
      Parameters:
      con - the connection to drop from the pending and read-after-live queues
    • processRead

      private void processRead(NTCPConnection con)
      Process everything read. Return read buffers back to the pool as we process them.
      Parameters:
      con - the connection whose read buffers are processed, ignored if null