- Type Parameters:
E- type of elements in this queue
- All Implemented Interfaces:
Serializable, Iterable<E>, Collection<E>, BlockingQueue<E>, Queue<E>
Implements the CoDel algorithm from RFC 8289 for managing queue congestion and controlling latency through packet dropping. Provides active queue management with automatic dropping when queue delay exceeds configurable thresholds.
Code and comments are derived from RFC 8289 appendix, implementing the standard CoDel algorithm with configurable target delay and interval parameters. Monitors queue sojourn time and drops packets when above target to maintain optimal throughput and latency characteristics.
Input methods are overridden to add timestamps for delay calculation. Output methods implement AQM behavior with comprehensive statistics tracking for performance monitoring.
- See Also:
Field Summary
Modifier and TypeFieldDescriptionprivate static final AtomicLongUnique ID generator for instances.private final intMaximum queue capacity.private final I2PAppContextSupplies the clock and the statistics.private intPackets dropped since going into drop state.private longTime to drop next packet.private booleanTrue if in drop state.private longTime when we'll declare we're above target (0 if below).private final longInstance ID.private longCoDel interval in ms.private final LogLogger for this class.private final StringQueue name.private longPer-request global for ease of use, locked by this.private longCoDel target delay in ms.private static final longHow long an item may sit before the queue counts as backlogged, in ms.private static final intQuote: A setting of 100 ms works well across a range of RTTs from 10 ms to 1 secondprivate static final intQuote: Below a target of 5 ms, utilization suffers for some conditions and traffic loads; above 5 ms there is very little or no improvement in utilization.private static final CopyOnWriteArrayList<WeakReference<CoDelBlockingQueue>> Active instances for dynamic tuning — weak refs prevent leak when tunnels expire.private static final intMost items dropped in one control-law call.static final StringProperty overriding the interval in ms between CoDel drop decisions.static final StringProperty overriding the CoDel target delay in ms.private static final longFixed id, the format is never serialized across versions.private final StringRateStat name for message delay.Constructor Summary
ConstructorsConstructorDescriptionCoDelBlockingQueue(I2PAppContext ctx, String name, int capacity) Queue with default CoDel target and interval from properties.CoDelBlockingQueue(I2PAppContext ctx, String name, int capacity, int target, int interval) Queue with explicit CoDel target and interval.Method Summary
Modifier and TypeMethodDescriptionbooleanAdd an element, recording the enqueue time.voidclear()Clear the queue and reset CoDel state.private EApply CoDel control to the given entry, dropping if needed.private voidcontrol_law(long t) Caller must synch on this.private Edeque()Dequeue the next element, applying CoDel drops as needed.voiddispose()Remove this instance from the tuning list.intdrainAllTo(Collection<? super E> c) Drains all, without updating stats or dropping.intdrainTo(Collection<? super E> c) Updates stats and possibly drops while draining.intdrainTo(Collection<? super E> c, int maxElements) Updates stats and possibly drops while draining.private voidDiscards a backlogged item and logs it.static voidRemove cleared (GC'd) weak references from the list.longThe CoDel measuring interval, the sliding window over which the minimum queuing delay is observed.longThe CoDel target queuing delay, the delay considered acceptable before the queue is judged congested.booleanHas the head of the queue been waiting too long, or is the queue almost full?booleanOffer an element, recording the enqueue time.booleanOffer an element, recording the enqueue time.poll()Poll the next element, applying CoDel drops as needed.voidPut an element, recording the enqueue time.voidsetInterval(long interval) Current CoDel interval in ms.voidsetTarget(long target) Current CoDel target delay in ms.take()Take the next element, applying CoDel drops as needed.static voidupdateAllIntervals(long interval) Update interval on all active CoDelBlockingQueue instances.static voidupdateAllTargets(long target) Update target on all active CoDelBlockingQueue instances.private booleanupdateVars(E entry) Caller must synch on this.Methods inherited from class LinkedBlockingQueue
contains, forEach, iterator, peek, poll, remainingCapacity, remove, removeAll, removeIf, retainAll, size, spliterator, toArray, toArray, toStringMethods inherited from class AbstractQueue
addAll, element, removeMethods inherited from class AbstractCollection
containsAll, isEmptyMethods inherited from class Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface Collection
equals, hashCode, parallelStream, stream, toArray
Field Details
serialVersionUID
private static final long serialVersionUIDFixed id, the format is never serialized across versions.- See Also:
_context
Supplies the clock and the statistics._log
Logger for this class._name
Queue name._capacity
private final int _capacityMaximum queue capacity._first_above_time
private long _first_above_timeTime when we'll declare we're above target (0 if below). One of four CoDel state variables below, all locked by this._drop_next
private long _drop_nextTime to drop next packet._count
private int _countPackets dropped since going into drop state._dropping
private boolean _droppingTrue if in drop state._now
private long _nowPer-request global for ease of use, locked by this.__id
Unique ID generator for instances._id
private final long _idInstance ID.INSTANCES
Active instances for dynamic tuning — weak refs prevent leak when tunnels expire.PROP_CODEL_TARGET
Property overriding the CoDel target delay in ms.- See Also:
PROP_CODEL_INTERVAL
Property overriding the interval in ms between CoDel drop decisions.- See Also:
DEFAULT_CODEL_TARGET
private static final int DEFAULT_CODEL_TARGETQuote: Below a target of 5 ms, utilization suffers for some conditions and traffic loads; above 5 ms there is very little or no improvement in utilization.I2P: Lowered to 5ms for reduced latency in high-performance scenarios.
- See Also:
_target
private volatile long _targetCoDel target delay in ms.DEFAULT_CODEL_INTERVAL
private static final int DEFAULT_CODEL_INTERVALQuote: A setting of 100 ms works well across a range of RTTs from 10 ms to 1 secondI2P: Lowered to 50ms for faster reaction to congestion.
- See Also:
_interval
private volatile long _intervalCoDel interval in ms.STAT_DELAY
RateStat name for message delay.BACKLOG_TIME
private static final long BACKLOG_TIMEHow long an item may sit before the queue counts as backlogged, in ms.MAX_DROPS_PER_CALL
private static final int MAX_DROPS_PER_CALLMost items dropped in one control-law call.- See Also:
Constructor Details
CoDelBlockingQueue
Queue with default CoDel target and interval from properties.- Parameters:
ctx- the I2P application contextname- name for this queue instancecapacity- the maximum capacity of the queue
CoDelBlockingQueue
Queue with explicit CoDel target and interval.- Parameters:
ctx- the I2P application contextname- the label identifying this queue in log messagescapacity- the maximum capacity of the queuetarget- the CoDel target delay in ms, held without delayinterval- how long above target to start dropping (ms)
Method Details
dispose
public void dispose()Remove this instance from the tuning list. Called when the owning tunnel/queue is no longer active.expungeStaleInstances
public static void expungeStaleInstances()Remove cleared (GC'd) weak references from the list.updateAllTargets
public static void updateAllTargets(long target) Update target on all active CoDelBlockingQueue instances.- Parameters:
target- the new target delay in ms
updateAllIntervals
public static void updateAllIntervals(long interval) Update interval on all active CoDelBlockingQueue instances.- Parameters:
interval- the new interval in ms
getTarget
public long getTarget()The CoDel target queuing delay, the delay considered acceptable before the queue is judged congested.- Returns:
- the target delay in ms
setTarget
public void setTarget(long target) Current CoDel target delay in ms.- Parameters:
target- the new target delay in ms
getInterval
public long getInterval()The CoDel measuring interval, the sliding window over which the minimum queuing delay is observed.- Returns:
- the interval in ms
setInterval
public void setInterval(long interval) Current CoDel interval in ms.- Parameters:
interval- the new interval in ms
add
Add an element, recording the enqueue time.offer
offer
Offer an element, recording the enqueue time.- Specified by:
offerin interfaceBlockingQueue<E extends CDQEntry>- Overrides:
offerin classLinkedBlockingQueue<E extends CDQEntry>- Throws:
InterruptedException
put
Put an element, recording the enqueue time.- Specified by:
putin interfaceBlockingQueue<E extends CDQEntry>- Overrides:
putin classLinkedBlockingQueue<E extends CDQEntry>- Throws:
InterruptedException
clear
public void clear()Clear the queue and reset CoDel state.- Specified by:
clearin interfaceCollection<E extends CDQEntry>- Overrides:
clearin classLinkedBlockingQueue<E extends CDQEntry>
take
Take the next element, applying CoDel drops as needed.- Specified by:
takein interfaceBlockingQueue<E extends CDQEntry>- Overrides:
takein classLinkedBlockingQueue<E extends CDQEntry>- Throws:
InterruptedException
poll
drainTo
Updates stats and possibly drops while draining. May exit early if a CoDel drop occurs (poll()returns null for a dropped item).- Specified by:
drainToin interfaceBlockingQueue<E extends CDQEntry>- Overrides:
drainToin classLinkedBlockingQueue<E extends CDQEntry>
drainTo
Updates stats and possibly drops while draining. May exit early if a CoDel drop occurs (poll() returns null for a dropped item).- Specified by:
drainToin interfaceBlockingQueue<E extends CDQEntry>- Overrides:
drainToin classLinkedBlockingQueue<E extends CDQEntry>
drainAllTo
Drains all, without updating stats or dropping.- Parameters:
c- the collection to drain into- Returns:
- the number of elements drained
isBacklogged
public boolean isBacklogged()Has the head of the queue been waiting too long, or is the queue almost full?- Returns:
- true if the queue is backlogged
updateVars
Caller must synch on this.- Parameters:
entry- may be null- Returns:
- true if the sojourn time has been above target long enough to drop
deque
Dequeue the next element, applying CoDel drops as needed.- Returns:
- if null, call again
- Throws:
InterruptedException- if the calling thread is interrupted while waiting
codel
drop
Discards a backlogged item and logs it.- Parameters:
entry- the item to drop
control_law
private void control_law(long t) Caller must synch on this.- Parameters:
t- the current time in ms, the base for the next drop time