java.lang.Object
net.i2p.router.tunnel.TunnelGatewayPumper
- All Implemented Interfaces:
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.
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static classSpecial poison pill PumpedTunnelGateway to signal thread shutdown.private classTimer event for requeuing backlogged gateways after delay.Field Summary
FieldsModifier and TypeFieldDescriptionprivate final AtomicIntegerTracks how many pumper threads are actively processing (not parked on take()).private final Set<PumpedTunnelGateway> Gateways whose sends are falling behind, waiting to be requeued.private final RouterContextThe router context.private final Set<PumpedTunnelGateway> Gateways already in the wants queue.private static TunnelGatewayPumperRunning instance for dynamic resize from Tuner.private final LogLogger for the gateway pumper.private static intMost pump threads allowed.private intPump threads running now.private static intCapacity of the wants queue.private static longWait time to requeue a backlogged gateway to allow task processing to catch up.private booleanTrue to stop the pump threads.private static final AtomicIntegerSequence number of the pump threads.The running pump threads.private BlockingQueue<PumpedTunnelGateway> Gateways waiting for a pump thread, a dynamically resizable queue.private static final longMax time to wait for pumper queue space before dropping the request.private static final intSpecial poison pill constant used to signal termination.Constructor Summary
ConstructorsConstructorDescriptionCreates a new TunnelGatewayPumper and starts configured number of pump threads.Method Summary
Modifier and TypeMethodDescriptionstatic voidadjustRunningThreads(int newCount) Adjust the running instance's thread count.voidadjustThreads(int newCount) Dynamically adjust the number of pumper threads.intThe number of pumper threads actively processing (not parked on take()).doubleThe pumper pool utilization as a ratio (0.0-1.0).static intThe current max pumper threads.static intThe current pumper queue capacity.static longHow long a message waits in the reschedule queue before it is tried again.static doubleThe pumper pool utilization as a ratio (0.0-1.0).private voidHandle re-adding a gateway to the queue after a short delay if pumping was incomplete.voidresizeQueue(int newCapacity) Resize the pumper queue.static voidresizeRunningQueue(int newCapacity) Resize the running instance's queue.voidrun()Primary thread run loop.private voidrun2()Pumps gateways from the wants queue.static voidsetMaxPumpers(int max) The max pumper threads.static voidsetQueueCapacity(int capacity) The pumper queue capacity.static voidsetRequeueTime(long ms) Change the reschedule delay, holding it inside the supported range.voidRequests a graceful stop for pump threads.voidRequests pumping for a gateway.
Field Details
_context
The router context._log
Logger for the gateway pumper._wantsPumping
Gateways waiting for a pump thread, a dynamically resizable queue._inQueue
Gateways already in the wants queue._backlogged
Gateways whose sends are falling behind, waiting to be requeued._threads
_activeCount
Tracks how many pumper threads are actively processing (not parked on take())._stop
private volatile boolean _stopTrue to stop the pump threads._queueCapacity
private static volatile int _queueCapacityCapacity of the wants queue._maxPumpers
private static volatile int _maxPumpersMost pump threads allowed._threadNum
Sequence number of the pump threads._instance
Running instance for dynamic resize from Tuner._pumpers
private volatile int _pumpersPump threads running now._requeueTime
private static volatile long _requeueTimeWait time to requeue a backlogged gateway to allow task processing to catch up.OFFER_TIMEOUT_MS
private static final long OFFER_TIMEOUT_MSMax 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_PTGSpecial poison pill constant used to signal termination.- See Also:
Constructor Details
TunnelGatewayPumper
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; callresizeQueue(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
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
run2
private void run2()Pumps gateways from the wants queue.handleRequeue
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