java.lang.Object
net.i2p.i2ptunnel.I2PTunnelHTTPClient.WarmSlot
Enclosing class:
I2PTunnelHTTPClient

static final class I2PTunnelHTTPClient.WarmSlotextends Object
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 Classes
    Modifier and Type
    Class
    Description
    (package private) static enum 
    Slot lifecycle.
  • Constructor Summary

    Constructors
    Constructor
    Description
    WarmSlot(java.util.function.Consumer<Hash> releaser)
    At most once per permit the slot has held.
  • Method Summary

    All Methods
    Modifier and Type
    Method
    Description
    (package private) void
    Retire the slot: close an installed socket if any and release the permit the slot still holds.
    (package private) long
    The generation of the most recent warm-connect attempt, which is how a late completion is matched against the attempt that is still current.
    The slot's lifecycle state, for tests and diagnostics.
    (package private) boolean
    install(long gen, I2PSocket sock)
    Install the socket produced by the attempt for gen.
    (package private) boolean
    Whether the slot is free to start a warm connect: still OPEN, with no attempt in flight and no socket installed.
    (package private) long
    open(Hash destHash, int port, Hash permit)
    Begin a warm-connect attempt, taking a lease on permit.
    (package private) void
    Give back the lease held by the current attempt (failed, rejected, or interrupted connect).
    (package private) I2PSocket
    take(Hash destHash, int port)
    Take an installed socket for the request.

    Methods inherited from class Object

    clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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

      long open(Hash destHash, int port, Hash permit)
      Begin a warm-connect attempt, taking a lease on permit. 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 to
      port - 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

      boolean install(long gen, I2PSocket sock)
      Install the socket produced by the attempt for gen. 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 close sock and call releaseLease() when this returns false.
      Parameters:
      gen - generation returned by open(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

      I2PSocket take(Hash destHash, int port)
      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 to I2PTunnelHTTPClient.WarmSlot.State.OPEN so a fresh attempt can be opened.
      Parameters:
      destHash - destination the caller needs
      port - 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 when install(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

      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