java.lang.Object
java.lang.Thread
net.i2p.util.I2PThread
net.i2p.router.crypto.ratchet.Elg2KeyFactory
- All Implemented Interfaces:
Runnable, KeyFactory
Threaded factory for pre-calculating Elligator2-encoded X25519 key pairs to prevent Diffie-Hellman computation bottlenecks in critical paths
Try to keep DH pairs at the ready. It's important to do this in a separate thread, because if we run out, the pairs are generated in the NTCP Pumper thread, and it can fall behind.
Nested Class Summary
Nested classes/interfaces inherited from class I2PThread
I2PThread.OOMEventListenerModifier and TypeClassDescriptionstatic interfaceListener for out-of-memory events.Nested classes/interfaces inherited from class Thread
Thread.Builder, Thread.State, Thread.UncaughtExceptionHandlerField Summary
FieldsModifier and TypeFieldDescriptionprivate intDelay between precalculation batches.private longDelay before checking the queue again.private final RouterContextThe router context.private final Elligator2Mapping that makes the Decloak keys.private final AtomicIntegerTimes the queue was found empty.private booleanTrue while the factory thread runs.private final LinkedBlockingQueue<Elg2KeyPair> Queue of the precalculated key pairs.private final LogLogger for the key precalculation.private intKeys kept in the queue at most.private intKeys kept in the queue at least.private final AtomicIntegerKeys taken from the queue.private static final intDefault precalculation delay, in ms.private static final intDefault maximum queue size.private static final intDefault minimum queue size.private static final intLargest queue size allowed.private static Elg2KeyFactoryThe factory created last, for reuse.private static final StringConfig key for the precalculation delay.private static final StringConfig key for the maximum queue size.private static final StringConfig key for the minimum queue size.private final booleanTrue to return unused keys to the X25519 factory.Fields inherited from class Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITYConstructor Summary
ConstructorsConstructorDescriptionCreates factory and starts background key precomputation thread.Method Summary
Modifier and TypeMethodDescriptionprivate final booleanaddKeys(Elg2KeyPair kp) Adds a precomputed key pair to the pool.static Elg2KeyFactoryLast created instance.getKeys()Pulls a prebuilt keypair from the queue, or if not available, construct a new one.intCurrent maximum precalc queue size.intCurrent minimum precalc queue size.intgetSize()Current number of precalc keys queued.private Elg2KeyPairprecalc()Generates one key pair.voidCalled by the Tuner every 15 seconds.voidUnused DH key builder; keys are discarded, not pooled.voidrun()Precalc queue fill loop.private voidrun2()Runs the precalculation loop.voidsetMaxSize(int max) Maximum precalc queue size.voidsetMinSize(int min) Minimum precalc queue size.voidshutdown()Note that this stops the singleton precalc thread.Methods inherited from class I2PThread
addOOMEventListener, fireOOM, removeOOMEventListener, startModifier and TypeMethodDescriptionstatic voidRegister a new component that wants notification of OOM events.protected voidfireOOM(OutOfMemoryError oom) Notify listeners that an OutOfMemoryError occurred.static voidUnregister a component that wants notification of OOM events.voidstart()Overridden to provide useful info to users on OOM, and to prevent shutting down the whole JVM for what is most likely not a heap issue.Methods inherited from class Thread
activeCount, clone, currentThread, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, isVirtual, join, join, join, join, ofPlatform, ofVirtual, onSpinWait, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, sleep, startVirtualThread, threadId, toString, yield
Field Details
lastInstance
The factory created last, for reuse._context
The router context._log
Logger for the key precalculation._elg2
Mapping that makes the Decloak keys._minSize
private volatile int _minSizeKeys kept in the queue at least._maxSize
private volatile int _maxSizeKeys kept in the queue at most._calcDelay
private volatile int _calcDelayDelay between precalculation batches._keys
Queue of the precalculated key pairs._isRunning
private volatile boolean _isRunningTrue while the factory thread runs._checkDelay
private long _checkDelayDelay before checking the queue again._emptyCount
Times the queue was found empty._usedCount
Keys taken from the queue.PROP_DH_PRECALC_MIN
Config key for the minimum queue size.- See Also:
PROP_DH_PRECALC_MAX
Config key for the maximum queue size.- See Also:
PROP_DH_PRECALC_DELAY
Config key for the precalculation delay.- See Also:
DEFAULT_DH_PRECALC_MIN
private static final int DEFAULT_DH_PRECALC_MINDefault minimum queue size.DEFAULT_DH_PRECALC_MAX
private static final int DEFAULT_DH_PRECALC_MAXDefault maximum queue size.DEFAULT_DH_PRECALC_DELAY
private static final int DEFAULT_DH_PRECALC_DELAYDefault precalculation delay, in ms.- See Also:
HARD_MAX
RETURN_UNUSED_TO_XDH
private final boolean RETURN_UNUSED_TO_XDHTrue to return unused keys to the X25519 factory.
Constructor Details
Elg2KeyFactory
Creates factory and starts background key precomputation thread.- Parameters:
ctx- the router context supplying the log, statistics and precomputation thread
Method Details
getInstance
Last created instance.- Returns:
- the most recently constructed factory, or null if none was built
getMinSize
public int getMinSize()Current minimum precalc queue size.- Returns:
- the min size
setMinSize
public void setMinSize(int min) Minimum precalc queue size.- Parameters:
min- the requested floor, raised to 1 if less than that is passed
getMaxSize
public int getMaxSize()Current maximum precalc queue size.- Returns:
- the max size
setMaxSize
public void setMaxSize(int max) Maximum precalc queue size.- Parameters:
max- the requested ceiling, raised to the current minimum if it is lower
getSize
public int getSize()Current number of precalc keys queued.- Returns:
- the number of precomputed keys waiting in the queue
refreshPoolSize
public void refreshPoolSize()Called by the Tuner every 15 seconds. Adjusts max pool size based on demand.shutdown
public void shutdown()Note that this stops the singleton precalc thread. You don't want to do this if there are multiple routers in the JVM. Fix this if you care. SeeRouter.shutdown(int).run
run2
private void run2()Runs the precalculation loop.getKeys
Pulls a prebuilt keypair from the queue, or if not available, construct a new one.- Specified by:
getKeysin interfaceKeyFactory- Returns:
- a prebuilt keypair taken from the queue, or a freshly generated one if the queue was empty
precalc
returnUnused
Unused DH key builder; keys are discarded, not pooled.- Parameters:
kp- the key pair to discard; currently unused, as nothing is returned to the pool
addKeys
Adds a precomputed key pair to the pool.- Parameters:
kp- the key pair to add- Returns:
- true if successful, false if full