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

class NTCPSendFinisherextends Object
Handles asynchronous post-send processing of OutNetMessage using a dynamically resizable thread pool executor with a bounded queue and backpressure. Provides efficient message serialization and prevents resource exhaustion.
  • Field Details

    • _maxThreads

      private static volatile int _maxThreads
      Threads in the finisher pool.
    • _queueCapacity

      private static volatile int _queueCapacity
      Events queued for the finishers.
    • _context

      private final I2PAppContext _context
      The app context.
    • _transport

      private final NTCPTransport _transport
      The transport whose sends are finished.
    • _log

      private final Log _log
      Logger for the send finisher.
    • _count

      private static final AtomicInteger _count
      Sequence number of the finish events.
    • _executor

      private ThreadPoolExecutor _executor
      Pool running the finish events.
    • RATES

      private static final long[] RATES
      Time windows of the send rate stats.
  • Constructor Details

    • NTCPSendFinisher

      public NTCPSendFinisher(I2PAppContext context, NTCPTransport transport)
      Create a new send finisher.
      Parameters:
      context - the I2P app context
      transport - the NTCP transport
  • Method Details

    • getMaxThreads

      public static int getMaxThreads()
      Current max send finisher threads.
      Returns:
      the max thread count
    • setMaxThreads

      public static void setMaxThreads(int threads)
      Max send finisher threads for new instances.
      Parameters:
      threads - the new max thread count
    • getQueueCapacity

      public static int getQueueCapacity()
      Current send finisher queue capacity.
      Returns:
      the queue capacity
    • setQueueCapacity

      public static void setQueueCapacity(int capacity)
      Send finisher queue capacity for new instances.
      Parameters:
      capacity - the new queue capacity
    • start

      public void start()
      Starts the thread pool executor for processing send finish tasks.
    • stop

      public void stop()
      Stops the thread pool executor, waiting briefly for termination.
    • adjustThreads

      public void adjustThreads(int newMax)
      Adjusts the thread pool size for the running executor. If the pool is not running, this is a no-op (new size takes effect on next start).
      Parameters:
      newMax - the new max thread count
    • getQueueSize

      public int getQueueSize()
      Current queue size (pending send-finish tasks).
      Returns:
      current queue depth, or 0 if not running
    • getActiveCount

      public int getActiveCount()
      Number of threads currently processing tasks.
      Returns:
      active thread count, or 0 if not running
    • getPoolSize

      public int getPoolSize()
      Current pool size (total threads).
      Returns:
      pool size, or 0 if not running
    • getUtilization

      public double getUtilization()
      Send finisher pool utilization as a ratio (0.0-1.0). Returns NaN if pool not started.
      Returns:
      the utilization ratio
    • add

      public void add(OutNetMessage msg)
      Adds a message to the finishing queue to call afterSend asynchronously. If the executor is stopped or saturated, falls back to caller running the task.
      Parameters:
      msg - the message to process after sending