java.lang.Object
net.i2p.router.tunnel.TunnelGateway
net.i2p.router.tunnel.PumpedTunnelGateway
Direct Known Subclasses:
ThrottledPumpedTunnelGateway, TunnelGatewayPumper.PoisonPTG

class PumpedTunnelGatewayextends TunnelGateway
This class represents a tunnel gateway with multiple hops that accepts messages, then coalesces and fragments them before sending them down the tunnel.

The processing flow is:

  1. add an I2NPMessage (and optionally, the target router or tunnel)
  2. messages queue in this PumpedTunnelGateway's internal _prequeue
  3. QueuePreprocessor pulls PendingGatewayMessages from _prequeue and processes them
  4. Fragments are sent to Sender, encrypted, and delivered to the Receiver
  5. Receiver directs the message to the next tunnel hop or endpoint

This class uses specialized CoDel queues to manage queue delays and bufferbloat. The outbound gateway uses a priority queue, while inbound uses a bounded non-priority queue.

Thread safety:

  • _prequeue is a thread-safe blocking queue managing pending messages
  • _queue is synchronized for modifications and preprocessing
  • Expiration pruning is done outside locks to minimize contention
  • The pump(List) method is safe for concurrent invocation by pumper threads
  • Field Details

    • _prequeue

      private final BlockingQueue<PendingGatewayMessage> _prequeue
      Messages waiting to enter the tunnel queue.
    • _pumper

      private final TunnelGatewayPumper _pumper
      The thread pumping messages into the tunnel.
    • _isInbound

      public final boolean _isInbound
      Whether this gateway is inbound (vs outbound).
    • _nextHop

      private final Hash _nextHop
      The hash of the next hop.
    • _queueSizeBatch

      private final RateStatMeanBatch _queueSizeBatch
      Gateway queue depths observed since the last flush, reported as one mean sample per pump rather than one sample per message. The stat is a level the Tuner and the graphs read as a per-period average, so the mean is the same quantity; sampling it per message cost a stat lookup and one monitor per rate period for every message enqueued, which on a busy gateway is thousands a second.
    • _maxObMsgsPerPump

      private static volatile int _maxObMsgsPerPump
      Most outbound messages pumped at once.
    • _maxIbMsgsPerPump

      private static volatile int _maxIbMsgsPerPump
      Most inbound messages pumped at once.
    • INITIAL_OB_QUEUE

      private static final int INITIAL_OB_QUEUE
      Initial size of the outbound queue.
    • MAX_IB_QUEUE

      private static final int MAX_IB_QUEUE
      Largest size of the inbound queue.
    • _statsCreated

      private static volatile boolean _statsCreated
      True once the stats were created.
    • PROP_MAX_OB_MSGS_PER_PUMP

      public static final String PROP_MAX_OB_MSGS_PER_PUMP
      Property key for max outbound messages per pump.
      See Also:
    • PROP_MAX_IB_MSGS_PER_PUMP

      public static final String PROP_MAX_IB_MSGS_PER_PUMP
      Property key for max inbound messages per pump.
      See Also:
    • PROP_INITIAL_OB_QUEUE

      public static final String PROP_INITIAL_OB_QUEUE
      Property key for initial outbound queue size.
      See Also:
    • PROP_MAX_IB_QUEUE

      public static final String PROP_MAX_IB_QUEUE
      Property key for max inbound queue size.
      See Also:
  • Constructor Details

    • PumpedTunnelGateway

      public PumpedTunnelGateway(RouterContext context, TunnelGateway.QueuePreprocessor preprocessor, TunnelGateway.Sender sender, TunnelGateway.Receiver receiver, TunnelGatewayPumper pumper)
      A gateway for a single tunnel hop.

      Outbound gateways use an unbounded priority queue; inbound gateways use a bounded blocking queue.

      Parameters:
      context - RouterContext supplying the stat manager and clock the gateway queues are built against
      preprocessor - QueuePreprocessor responsible for preprocessing messages
      sender - Sender responsible for encrypting and delivering messages
      receiver - Receiver that consumes encrypted messages for forwarding
      pumper - TunnelGatewayPumper managing pumping threads
  • Method Details

    • getMaxObMsgsPerPump

      public static int getMaxObMsgsPerPump()
      The cap the pump applies to outbound messages, read once per pump pass so a single busy iteration cannot monopolise the thread.
      Returns:
      the cap on outbound messages handled in one pump iteration, 8 to 1024
    • setMaxObMsgsPerPump

      public static void setMaxObMsgsPerPump(int val)
      The maximum outbound messages per pump.
      Parameters:
      val - the requested cap, clamped into the range 8 to 1024
    • getMaxIbMsgsPerPump

      public static int getMaxIbMsgsPerPump()
      The maximum inbound messages per pump.
      Returns:
      the max ib msgs per pump
    • setMaxIbMsgsPerPump

      public static void setMaxIbMsgsPerPump(int val)
      The maximum inbound messages per pump.
      Parameters:
      val - the requested cap, clamped into the range 8 to 1024
    • add

      public boolean add(I2NPMessage msg, Hash toRouter, TunnelId toTunnel)
      Adds a message to be sent down the tunnel. If the prequeue is full, the message is dropped and a statistic is updated.

      This method is optimized for outbound gateways and called by TPTG for inbound.

      Overrides:
      add in class TunnelGateway
      Parameters:
      msg - the message to send
      toRouter - optional target router
      toTunnel - optional target tunnel
      Returns:
      true if the message was accepted, false if the prequeue is full
    • add

      protected boolean add(PendingGatewayMessage cur)
      Adds a PendingGatewayMessage to the prequeue and notifies the pumper to process it.
      Parameters:
      cur - the PendingGatewayMessage to add
      Returns:
      true if the message was accepted, false if the prequeue is full
    • pump

      public boolean pump(List<PendingGatewayMessage> queueBuf)
      Pumps messages from the internal prequeue into the processing queue.

      This method tries to drain a bounded number of messages from _prequeue into the provided queueBuf, then adds them to the internal _queue for preprocessing and fragmentation.

      Expired messages are removed after preprocessing, outside the synchronization block, to reduce contention.

      Parameters:
      queueBuf - an empty list used as a temporary buffer for messages (will be cleared before return)
      Returns:
      true if there are still messages remaining in _prequeue and the caller should requeue this gateway