java.lang.Object
net.i2p.router.tunnel.TunnelGateway
net.i2p.router.tunnel.PumpedTunnelGateway
- Direct Known Subclasses:
ThrottledPumpedTunnelGateway, TunnelGatewayPumper.PoisonPTG
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:
- add an I2NPMessage (and optionally, the target router or tunnel)
- messages queue in this PumpedTunnelGateway's internal _prequeue
- QueuePreprocessor pulls PendingGatewayMessages from _prequeue and processes them
- Fragments are sent to Sender, encrypted, and delivered to the Receiver
- 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
Nested Class Summary
Nested classes/interfaces inherited from class TunnelGateway
TunnelGateway.DelayedFlush, TunnelGateway.QueuePreprocessor, TunnelGateway.Receiver, TunnelGateway.SenderModifier and TypeClassDescriptionprotected classDelayed flush timer event.static interfaceInterface for preprocessing queued tunnel messages.static interfaceInterface for receiving encrypted tunnel data.static interfaceInterface for sending preprocessed tunnel data.Field Summary
Modifier and TypeFieldDescriptionfinal booleanWhether this gateway is inbound (vs outbound).private static intMost inbound messages pumped at once.private static intMost outbound messages pumped at once.private final HashThe hash of the next hop.private final BlockingQueue<PendingGatewayMessage> Messages waiting to enter the tunnel queue.private final TunnelGatewayPumperThe thread pumping messages into the tunnel.private final RateStatMeanBatchGateway queue depths observed since the last flush, reported as one mean sample per pump rather than one sample per message.private static booleanTrue once the stats were created.private static final intInitial size of the outbound queue.private static final intLargest size of the inbound queue.static final StringProperty key for initial outbound queue size.static final StringProperty key for max inbound messages per pump.static final StringProperty key for max inbound queue size.static final StringProperty key for max outbound messages per pump.Fields inherited from class TunnelGateway
_context, _delayedFlush, _lastFlush, _log, _messagesSent, _preprocessor, _queue, _receiver, _senderModifier and TypeFieldDescriptionprotected final RouterContextThe router context.protected final TunnelGateway.DelayedFlushThe delayed flush timer.protected longThe last flush time.protected final LogThe log instance.protected intThe messages sent count.protected final TunnelGateway.QueuePreprocessorThe queue preprocessor.protected final List<PendingGatewayMessage> The message queue.protected final TunnelGateway.ReceiverThe message receiver, consuming encrypted messages for forwarding.protected final TunnelGateway.SenderThe message sender, encrypting and forwarding preprocessed messages.Constructor Summary
ConstructorsConstructorDescriptionPumpedTunnelGateway(RouterContext context, TunnelGateway.QueuePreprocessor preprocessor, TunnelGateway.Sender sender, TunnelGateway.Receiver receiver, TunnelGatewayPumper pumper) A gateway for a single tunnel hop.Method Summary
Modifier and TypeMethodDescriptionbooleanadd(I2NPMessage msg, Hash toRouter, TunnelId toTunnel) Adds a message to be sent down the tunnel.protected booleanadd(PendingGatewayMessage cur) Adds a PendingGatewayMessage to the prequeue and notifies the pumper to process it.static intThe maximum inbound messages per pump.static intThe cap the pump applies to outbound messages, read once per pump pass so a single busy iteration cannot monopolise the thread.booleanpump(List<PendingGatewayMessage> queueBuf) Pumps messages from the internal prequeue into the processing queue.static voidsetMaxIbMsgsPerPump(int val) The maximum inbound messages per pump.static voidsetMaxObMsgsPerPump(int val) The maximum outbound messages per pump.Methods inherited from class TunnelGateway
add, getMessagesSentModifier and TypeMethodDescriptionbooleanadd(TunnelGatewayMessage msg) Add a message to be sent down the tunnel, where we are the inbound gateway.intThe number of messages sent.
Field Details
_prequeue
Messages waiting to enter the tunnel queue._pumper
The thread pumping messages into the tunnel._isInbound
public final boolean _isInboundWhether this gateway is inbound (vs outbound)._nextHop
The hash of the next hop._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 _maxObMsgsPerPumpMost outbound messages pumped at once._maxIbMsgsPerPump
private static volatile int _maxIbMsgsPerPumpMost inbound messages pumped at once.INITIAL_OB_QUEUE
private static final int INITIAL_OB_QUEUEInitial size of the outbound queue.MAX_IB_QUEUE
private static final int MAX_IB_QUEUELargest size of the inbound queue._statsCreated
private static volatile boolean _statsCreatedTrue once the stats were created.PROP_MAX_OB_MSGS_PER_PUMP
Property key for max outbound messages per pump.- See Also:
PROP_MAX_IB_MSGS_PER_PUMP
Property key for max inbound messages per pump.- See Also:
PROP_INITIAL_OB_QUEUE
Property key for initial outbound queue size.- See Also:
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 againstpreprocessor- QueuePreprocessor responsible for preprocessing messagessender- Sender responsible for encrypting and delivering messagesreceiver- Receiver that consumes encrypted messages for forwardingpumper- 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
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:
addin classTunnelGateway- Parameters:
msg- the message to sendtoRouter- optional target routertoTunnel- optional target tunnel- Returns:
- true if the message was accepted, false if the prequeue is full
add
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
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