java.lang.Object
net.i2p.router.tunnel.TunnelGatewayPumper
All Implemented Interfaces:
Runnable

class TunnelGatewayPumperextends Object implements Runnable
TunnelGatewayPumper runs a pool of threads that process PumpedTunnelGateway instances which need messages pushed through preprocessing and sending.

This class is thread-safe and designed to manage concurrent producers (calling wantsPumping) and multiple consumer threads (pumper threads).

Shutdown coordination: - The stopPumping() method sets a stop flag and enqueues a special poison pill for each thread. - Threads detect the poison pill and exit cleanly. - Threads are interrupted and joined to ensure complete termination.

Thread safety notes: - Uses a LinkedBlockingQueue with bounded capacity for backpressure. - Uses a concurrent Set to track backlogged gateways for requeueing. - Non-blocking offer() with timeout prevents I2CP reader stalls. - Gateway drops are tracked via tunnel.pumperQueueFull stat.

  • Field Details

    • _context

      private final RouterContext _context
      The router context.
    • _log

      private final Log _log
      Logger for the gateway pumper.
    • _wantsPumping

      private volatile BlockingQueue<PumpedTunnelGateway> _wantsPumping
      Gateways waiting for a pump thread, a dynamically resizable queue.
    • _inQueue

      private final Set<PumpedTunnelGateway> _inQueue
      Gateways already in the wants queue.
    • _backlogged

      private final Set<PumpedTunnelGateway> _backlogged
      Gateways whose sends are falling behind, waiting to be requeued.
    • _threads

      private final List<Thread> _threads
      The running pump threads.
    • _activeCount

      private final AtomicInteger _activeCount
      Tracks how many pumper threads are actively processing (not parked on take()).
    • _stop

      private volatile boolean _stop
      True to stop the pump threads.
    • _queueCapacity

      private static volatile int _queueCapacity
      Capacity of the wants queue.
    • _maxPumpers

      private static volatile int _maxPumpers
      Most pump threads allowed.
    • _threadNum

      private static final AtomicInteger _threadNum
      Sequence number of the pump threads.
    • _instance

      private static volatile TunnelGatewayPumper _instance
      Running instance for dynamic resize from Tuner.
    • _pumpers

      private volatile int _pumpers
      Pump threads running now.
    • _requeueTime

      private static volatile long _requeueTime
      Wait time to requeue a backlogged gateway to allow task processing to catch up.
    • OFFER_TIMEOUT_MS

      private static final long OFFER_TIMEOUT_MS
      Max time to wait for pumper queue space before dropping the request. Prevents blocking the I2CP reader thread indefinitely.
      See Also:
    • POISON_PTG

      private static final int POISON_PTG
      Special poison pill constant used to signal termination.
      See Also:
  • Constructor Details

    • TunnelGatewayPumper

      public TunnelGatewayPumper(RouterContext ctx)
      Creates a new TunnelGatewayPumper and starts configured number of pump threads.
      Parameters:
      ctx - the router context
  • Method Details

    • getQueueCapacity

      public static int getQueueCapacity()
      The current pumper queue capacity.
      Returns:
      the queue capacity
    • setQueueCapacity

      public static void setQueueCapacity(int capacity)
      The pumper queue capacity. Does not resize existing queue; call resizeQueue(int) to apply to running instance.
      Parameters:
      capacity - the requested queue capacity, clamped to the range 64 to 4096
    • getMaxPumpers

      public static int getMaxPumpers()
      The current max pumper threads.
      Returns:
      the max pumpers
    • setMaxPumpers

      public static void setMaxPumpers(int max)
      The max pumper threads.
      Parameters:
      max - the requested pump thread count, clamped to the range 2 to 16
    • getActiveCount

      public int getActiveCount()
      The number of pumper threads actively processing (not parked on take()).
      Returns:
      the active count
    • getInstanceUtilization

      public double getInstanceUtilization()
      The pumper pool utilization as a ratio (0.0-1.0). Returns NaN if no pumpers are running.
      Returns:
      the instance utilization
    • resizeRunningQueue

      public static void resizeRunningQueue(int newCapacity)
      Resize the running instance's queue.
      Parameters:
      newCapacity - desired queue depth; outside 64 to 4096 the resize is ignored
    • adjustRunningThreads

      public static void adjustRunningThreads(int newCount)
      Adjust the running instance's thread count.
      Parameters:
      newCount - desired thread count
    • getRequeueTime

      public static long getRequeueTime()
      How long a message waits in the reschedule queue before it is tried again.
      Returns:
      the delay before a requeued message is tried again, in milliseconds
    • setRequeueTime

      public static void setRequeueTime(long ms)
      Change the reschedule delay, holding it inside the supported range.
      Parameters:
      ms - the requested retry delay in milliseconds, clamped to the range 10 to 200
    • getUtilization

      public static double getUtilization()
      The pumper pool utilization as a ratio (0.0-1.0). Returns NaN if not started.
      Returns:
      active pumpers divided by pool size, or NaN if none are running
    • resizeQueue

      public void resizeQueue(int newCapacity)
      Resize the pumper queue. Drains old queue into new one.
      Parameters:
      newCapacity - new queue capacity
    • adjustThreads

      public void adjustThreads(int newCount)
      Dynamically adjust the number of pumper threads. Adds threads if count increased, signals excess threads to stop if decreased.
      Parameters:
      newCount - desired thread count
    • stopPumping

      public void stopPumping()
      Requests a graceful stop for pump threads. Clears queues and signals all pump threads to terminate. Waits for thread termination before returning.
    • wantsPumping

      public void wantsPumping(PumpedTunnelGateway gw)
      Requests pumping for a gateway. Uses a short timeout to avoid blocking the I2CP reader thread when the pumper queue is full.
      Parameters:
      gw - the gateway to pump
    • run

      public void run()
      Primary thread run loop. Picks gateways from the queue and pumps messages.
      Specified by:
      run in interface Runnable
    • run2

      private void run2()
      Pumps gateways from the wants queue.
    • handleRequeue

      private void handleRequeue(PumpedTunnelGateway gw)
      Handle re-adding a gateway to the queue after a short delay if pumping was incomplete. Uses a concurrent set to avoid multiple requeues for same gateway.
      Parameters:
      gw - gateway to requeue