java.lang.Object
net.i2p.router.transport.ntcp.Writer
Pool of running threads which will transform the next I2NP message into something ready to be transferred over an NTCP connection, including encryption of the data read. Provides efficient message serialization and connection lifecycle coordination. Supports dynamic thread count adjustment via
adjustThreads().Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate classThread writing the assigned connections.Field Summary
FieldsModifier and TypeFieldDescriptionprivate final AtomicIntegerTracks how many runner threads are actively processing (not parked).private final Set<NTCPConnection> Connections being written by a runner.private final LogLogger for the NTCP writer.private final Set<NTCPConnection> Connections waiting for a runner.private final CopyOnWriteArrayList<Writer.Runner> The running writer threads.private static intNumber of writer threads.private static final AtomicIntegerSequence number of the writer threads.private final Set<NTCPConnection> Connections to write after the live ones.private static final intMost writer threads allowed.private static final intFewest writer threads allowed.Constructor Summary
ConstructorsMethod Summary
Modifier and TypeMethodDescriptionvoidDynamically adjust thread count to match the target.voidRemoves a closed connection from the queues.intNumber of threads currently processing (not parked).static intWriter thread count.doubleWriter pool utilization as a ratio (0.0-1.0).static voidsetThreadCount(int count) Writer thread count, bounded by MIN_THREADS-MAX_THREADS.private voidStarts one more writer thread if needed.voidstartWriting(int numWriters) Starts the given number of writer threads.voidStops all writer threads.voidwantsWrite(NTCPConnection con, String source) Registers a connection to be written.
Field Details
_log
Logger for the NTCP writer._pendingConnections
Connections waiting for a runner._liveWrites
Connections being written by a runner._writeAfterLive
Connections to write after the live ones._runners
The running writer threads._threadCount
private static volatile int _threadCountNumber of writer threads.MIN_THREADS
MAX_THREADS
_threadNum
Sequence number of the writer threads._activeCount
Tracks how many runner threads are actively processing (not parked).
Constructor Details
Writer
Writer thread pool for the given context.- Parameters:
ctx- the router context whose log manager supplies this pool's logger
Method Details
getThreadCount
public static int getThreadCount()Writer thread count.- Returns:
- the configured number of writer threads, always within MIN_THREADS to MAX_THREADS
getActiveCount
public int getActiveCount()Number of threads currently processing (not parked).- Returns:
- how many of the pooled writer threads are running rather than parked waiting for a connection
getUtilization
public double getUtilization()Writer pool utilization as a ratio (0.0-1.0). NaN if no writers are active (pool not started).- Returns:
- the fraction of the pooled writer threads that are running rather than parked
setThreadCount
public static void setThreadCount(int count) Writer thread count, bounded by MIN_THREADS-MAX_THREADS.- Parameters:
count- the requested thread count, clamped into the MIN_THREADS to MAX_THREADS range before being stored
startWriting
public void startWriting(int numWriters) Starts the given number of writer threads.- Parameters:
numWriters- how many additional writer threads to spin up; each one is added to the pool without stopping or replacing an existing runner
startRunner
private void startRunner()Starts one more writer thread if needed.adjustThreads
public void adjustThreads()Dynamically adjust thread count to match the target.stopWriting
public void stopWriting()Stops all writer threads.wantsWrite
Registers a connection to be written. High-frequency per-buffer path.- Parameters:
con- the connection whose next outbound buffer must be serialized and sent by this poolsource- a caller-supplied label naming the code that requested the write, used only for debug logging
connectionClosed
Removes a closed connection from the queues.- Parameters:
con- the connection that has closed and must no longer be drained or written