java.lang.Object
net.i2p.i2ptunnel.I2PTunnelHTTPClient.WarmSlot
- Enclosing class:
I2PTunnelHTTPClient
Request-scoped state machine for one speculative warm socket.
The slot leases a per-destination connection permit from open(Hash, int, Hash) until the socket is handed to the request by take(Hash, int) or the attempt is abandoned by releaseLease() / cancel(); the releaser callback fires at most once per acquisition. Each attempt carries a generation so a background connect that completes after the slot moved on cannot install its socket or leak its permit into a later attempt — the failure modes of a plain holder: late install after teardown, handing a socket to a different destination, and double or missing permit release. All state is guarded by one lock because the background connect races request teardown.
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static enumSlot lifecycle.Constructor Summary
ConstructorsMethod Summary
All MethodsModifier and TypeMethodDescription(package private) voidcancel()Retire the slot: close an installed socket if any and release the permit the slot still holds.(package private) longThe generation of the most recent warm-connect attempt, which is how a late completion is matched against the attempt that is still current.(package private) I2PTunnelHTTPClient.WarmSlot.StategetState()The slot's lifecycle state, for tests and diagnostics.(package private) booleanInstall the socket produced by the attempt forgen.(package private) booleanisIdle()Whether the slot is free to start a warm connect: still OPEN, with no attempt in flight and no socket installed.(package private) longBegin a warm-connect attempt, taking a lease onpermit.(package private) voidGive back the lease held by the current attempt (failed, rejected, or interrupted connect).(package private) I2PSocketTake an installed socket for the request.
Constructor Details
WarmSlot
WarmSlot(java.util.function.Consumer<Hash> releaser) At most once per permit the slot has held.- Parameters:
releaser- callback that returns a permit to the pool; invoked
Method Details
open
Begin a warm-connect attempt, taking a lease onpermit. The lease is owned by the attempt until it installs, fails, or is rejected —install(long, I2PSocket)on success,releaseLease()otherwise.- Parameters:
destHash- destination the attempt will connect toport- remote port (0 = default)permit- connection permit this attempt leases- Returns:
- the attempt generation, or -1 when the slot is not idle (cancelled, full, or another attempt in flight); on -1 the caller keeps ownership of
permit
install
Install the socket produced by the attempt forgen. Rejects a stale generation (the attempt already failed or the slot was cancelled) so a late completion can never surface as the request's socket. The caller must closesockand callreleaseLease()when this returns false.- Parameters:
gen- generation returned byopen(Hash, int, Hash)sock- the connected socket- Returns:
- true when installed (slot is now
I2PTunnelHTTPClient.WarmSlot.State.FULL), false when the attempt is stale or the slot retired
take
Take an installed socket for the request. On a destination/port match the socket is returned and its lease is released. The request already holds its own permit (heldPermitDest), so keeping the warm one would double-count. On a mismatch the socket is discarded rather than handed to a request it was not opened for, and the slot returns toI2PTunnelHTTPClient.WarmSlot.State.OPENso a fresh attempt can be opened.- Parameters:
destHash- destination the caller needsport- remote port the caller needs (0 = default)- Returns:
- the installed socket, or null when the slot is not full or the installed socket was for a different destination/port
releaseLease
void releaseLease()Give back the lease held by the current attempt (failed, rejected, or interrupted connect). No-op while a socket is installed. The installed connection keeps its permit until taken or cancelled — and no-op when nothing is held, so it is safe to call from every attempt exit path without counting.cancel
void cancel()Retire the slot: close an installed socket if any and release the permit the slot still holds. An attempt still in flight keeps its lease — it releases it itself wheninstall(long, I2PSocket)rejects it. Idempotent.isIdle
boolean isIdle()Whether the slot is free to start a warm connect: still OPEN, with no attempt in flight and no socket installed.- Returns:
- true when the slot is OPEN, no attempt is in flight, and no installed socket is held
getState
I2PTunnelHTTPClient.WarmSlot.State getState()The slot's lifecycle state, for tests and diagnostics.- Returns:
- the current lifecycle state
getGeneration
long getGeneration()The generation of the most recent warm-connect attempt, which is how a late completion is matched against the attempt that is still current.- Returns:
- the last issued attempt generation, or 0 before any open