java.lang.Object
net.i2p.router.transport.FIFOBandwidthRefiller
- All Implemented Interfaces:
Runnable
Bandwidth token refiller for FIFO-based bandwidth limiting.
This thread periodically refills bandwidth token buckets for the FIFOBandwidthLimiter. It runs independently to ensure tokens are available at regular intervals, preventing traffic starvation and maintaining consistent throughput.
Refill Strategy:
- Fixed interval token replenishment
- Configurable refill rates and amounts
- Separate inbound and outbound bandwidth tracking
- Participating bandwidth counter (since 0.8.12)
- Thread-safe operations with atomic counters
Configuration:
- Configurable inbound bandwidth limits (KBps)
- Configurable outbound bandwidth limits (KBps)
- Adjustable refill intervals and amounts
- System property integration for defaults
Thread Safety:
- Atomic counters for bandwidth tracking
- Thread-safe refill operations
- Non-blocking queue operations
Note: This class was originally created to provide a dedicated thread for bandwidth refilling when the main limiter needed thread resources. As of 0.8.12, it also includes participating bandwidth counter functionality.
Public only for properties and defaults.
Field Summary
Modifier and TypeFieldDescriptionprivate longHow frequently we check the config for updates.private final RouterContextThe router context.private intHow many KBps to allow during burst.private intHow many KBps to allow.private booleanTrue while the refiller runs.private longWhen we last checked the config for updates.private longWhen we last replenished the queue.private final FIFOBandwidthLimiterThe limiter whose queues are refilled.private final LogLogger for the bandwidth refiller.private intHow many KBps to allow during burst.private intHow many KBps to allow.private SyntheticREDQueueQueue estimating the outbound share.private SyntheticREDQueueQueue estimating the inbound share.private static longHow often we replenish the queues.static final intDefault burst seconds.static final intDefault inbound bandwidth, in KBps, unlimited is not allowed.static final intDefault inbound burst bandwidth.static final intCaution, do not make DEFAULT_OUTBOUND_BANDWIDTH * DEFAULT_SHARE_PCT > 32 without thinking about the implications (default connection limits, for example) of moving the default bandwidth class from L to M, or maybe adjusting bandwidth class boundaries.static final intDefault outbound burst bandwidth.static final intMax for reasonable Bloom filter false positive rate.private static final floatLargest share of the capacity we take.static final intFor now, until there is some tuning and safe throttling, we set the floor at this inbound (KBps).static final intFor now, until there is some tuning and safe throttling, we set the floor at this during burst (KBps).static final intFor now, until there is some tuning and safe throttling, we set the floor at this outbound (KBps).static final intFor now, until there is some tuning and safe throttling, we set the floor at this during burst (KBps).static final StringProperty for inbound bandwidth.static final StringProperty for inbound bandwidth peak.static final StringProperty for inbound burst bandwidth.static final StringProperty for outbound bandwidth.static final StringProperty for outbound bandwidth peak.static final StringProperty for outbound burst bandwidth.private static final floatShare of the limit the refill stays under.Constructor Summary
ConstructorsConstructorDescriptionFIFOBandwidthRefiller(RouterContext context, FIFOBandwidthLimiter limiter) Refiller keeping the given limiter's queues full.Method Summary
Modifier and TypeMethodDescriptionprivate voidRe-reads the bandwidth settings if they changed.(package private) intOut bandwidth.(package private) intIn bandwidth.(package private) intConfigured inbound burst bandwidth.(package private) intConfigured inbound bandwidth.(package private) intCap on bytes per second available to participating tunnels.(package private) intConfigured outbound burst bandwidth.(package private) intConfigured outbound bandwidth.static longHow often the queues are replenished.private intIn Bytes per second.(package private) booleanincrementParticipatingMessageBytes(int size, float factor) We intend to send traffic for a participating tunnel with the given size and adjustment factor.(package private) booleanincrementParticipatingMessageBytesIn(int size, float factor) We intend to receive traffic for a participating tunnel with the given size and adjustment factor.(package private) voidReset refill timing and reload configuration.voidrun()static voidsetReplenishFrequency(long ms) Set the replenish frequency, bounded to 5..200 ms.(package private) voidshutdown()Shut down the bandwidth refiller.private voidRefills the inbound burst queue.private voidUpdates the peak inbound rate estimate.private voidRefills the inbound bandwidth queue.private voidRefills the outbound burst queue.private voidUpdates the peak outbound rate estimate.private voidRefills the outbound bandwidth queue.private voidupdateParticipating(long now) Call a few times a minute to update the stats.private booleanupdateQueues(List<FIFOBandwidthLimiter.Request> buffer, long now) Refills the queues and reports any change.
Field Details
_log
Logger for the bandwidth refiller._context
The router context._limiter
The limiter whose queues are refilled._partBWE
Queue estimating the outbound share._partBWEIn
Queue estimating the inbound share._inboundKBytesPerSecond
private volatile int _inboundKBytesPerSecondHow many KBps to allow._outboundKBytesPerSecond
private volatile int _outboundKBytesPerSecondHow many KBps to allow._inboundBurstKBytesPerSecond
private volatile int _inboundBurstKBytesPerSecondHow many KBps to allow during burst._outboundBurstKBytesPerSecond
private volatile int _outboundBurstKBytesPerSecondHow many KBps to allow during burst._lastRefillTime
private volatile long _lastRefillTimeWhen we last replenished the queue._lastCheckConfigTime
private volatile long _lastCheckConfigTimeWhen we last checked the config for updates._configCheckPeriodMs
private volatile long _configCheckPeriodMsHow frequently we check the config for updates._isRunning
private volatile boolean _isRunningTrue while the refiller runs.PROP_INBOUND_BANDWIDTH
Property for inbound bandwidth.- See Also:
PROP_OUTBOUND_BANDWIDTH
Property for outbound bandwidth.- See Also:
PROP_INBOUND_BURST_BANDWIDTH
Property for inbound burst bandwidth.- See Also:
PROP_OUTBOUND_BURST_BANDWIDTH
Property for outbound burst bandwidth.- See Also:
PROP_INBOUND_BANDWIDTH_PEAK
Property for inbound bandwidth peak.- See Also:
PROP_OUTBOUND_BANDWIDTH_PEAK
Property for outbound bandwidth peak.- See Also:
DEFAULT_INBOUND_BANDWIDTH
public static final int DEFAULT_INBOUND_BANDWIDTHDefault inbound bandwidth, in KBps, unlimited is not allowed.- See Also:
DEFAULT_OUTBOUND_BANDWIDTH
public static final int DEFAULT_OUTBOUND_BANDWIDTHCaution, do not make DEFAULT_OUTBOUND_BANDWIDTH * DEFAULT_SHARE_PCT > 32 without thinking about the implications (default connection limits, for example) of moving the default bandwidth class from L to M, or maybe adjusting bandwidth class boundaries.- See Also:
DEFAULT_INBOUND_BURST_BANDWIDTH
public static final int DEFAULT_INBOUND_BURST_BANDWIDTHDefault inbound burst bandwidth.- See Also:
DEFAULT_OUTBOUND_BURST_BANDWIDTH
public static final int DEFAULT_OUTBOUND_BURST_BANDWIDTHDefault outbound burst bandwidth.- See Also:
DEFAULT_BURST_SECONDS
MIN_INBOUND_BANDWIDTH
public static final int MIN_INBOUND_BANDWIDTHFor now, until there is some tuning and safe throttling, we set the floor at this inbound (KBps).- See Also:
MIN_OUTBOUND_BANDWIDTH
public static final int MIN_OUTBOUND_BANDWIDTHFor now, until there is some tuning and safe throttling, we set the floor at this outbound (KBps).- See Also:
MIN_INBOUND_BANDWIDTH_PEAK
public static final int MIN_INBOUND_BANDWIDTH_PEAKFor now, until there is some tuning and safe throttling, we set the floor at this during burst (KBps).- See Also:
MIN_OUTBOUND_BANDWIDTH_PEAK
public static final int MIN_OUTBOUND_BANDWIDTH_PEAKFor now, until there is some tuning and safe throttling, we set the floor at this during burst (KBps).- See Also:
MAX_OUTBOUND_BANDWIDTH
public static final int MAX_OUTBOUND_BANDWIDTHMax for reasonable Bloom filter false positive rate. Do not increase without adding a new Bloom filter size! See util/DecayingBloomFilter and tunnel/BloomFilterIVValidator.MAX_SHARE_PERCENTAGE
private static final float MAX_SHARE_PERCENTAGELargest share of the capacity we take.- See Also:
SHARE_LIMIT_FACTOR
private static final float SHARE_LIMIT_FACTORShare of the limit the refill stays under.- See Also:
_replenishFrequency
private static volatile long _replenishFrequencyHow often we replenish the queues. The bandwidth limiter will get an update this often (ms).
Constructor Details
FIFOBandwidthRefiller
FIFOBandwidthRefiller(RouterContext context, FIFOBandwidthLimiter limiter) Refiller keeping the given limiter's queues full.- Parameters:
context- the router context supplying the log and the statslimiter- the limiter whose queues are replenished
Method Details
getReplenishFrequency
public static long getReplenishFrequency()How often the queues are replenished.- Returns:
- the replenish frequency in ms
setReplenishFrequency
public static void setReplenishFrequency(long ms) Set the replenish frequency, bounded to 5..200 ms.- Parameters:
ms- requested interval between queue replenishments, in milliseconds
shutdown
void shutdown()Shut down the bandwidth refiller.run
reinitialize
void reinitialize()Reset refill timing and reload configuration.updateQueues
Refills the queues and reports any change.- Parameters:
buffer- the queues to refillnow- the current time, in ms- Returns:
- true if any limit changed
checkConfig
private void checkConfig()Re-reads the bandwidth settings if they changed.updateInboundRate
private void updateInboundRate()Refills the inbound bandwidth queue.updateOutboundRate
private void updateOutboundRate()Refills the outbound bandwidth queue.updateInboundBurstRate
private void updateInboundBurstRate()Refills the inbound burst queue.updateOutboundBurstRate
private void updateOutboundBurstRate()Refills the outbound burst queue.updateInboundPeak
private void updateInboundPeak()Updates the peak inbound rate estimate.updateOutboundPeak
private void updateOutboundPeak()Updates the peak outbound rate estimate.getOutboundKBytesPerSecond
int getOutboundKBytesPerSecond()Configured outbound bandwidth.- Returns:
- the configured outbound bandwidth in KBps
getInboundKBytesPerSecond
int getInboundKBytesPerSecond()Configured inbound bandwidth.- Returns:
- the configured inbound bandwidth in KBps
getOutboundBurstKBytesPerSecond
int getOutboundBurstKBytesPerSecond()Configured outbound burst bandwidth.- Returns:
- the configured outbound burst bandwidth in KBps
getInboundBurstKBytesPerSecond
int getInboundBurstKBytesPerSecond()Configured inbound burst bandwidth.- Returns:
- the configured inbound burst bandwidth in KBps
incrementParticipatingMessageBytes
boolean incrementParticipatingMessageBytes(int size, float factor) We intend to send traffic for a participating tunnel with the given size and adjustment factor. Returns true if the message can be sent within the current share bandwidth limits, or false if it should be dropped.- Parameters:
size- the message size in bytesfactor- multiplier of size for the drop calculation, 1 for no adjustment- Returns:
- true for accepted, false for drop
incrementParticipatingMessageBytesIn
boolean incrementParticipatingMessageBytesIn(int size, float factor) We intend to receive traffic for a participating tunnel with the given size and adjustment factor. Returns true if the message can be accepted within the current share bandwidth limits, or false if it should be dropped.- Parameters:
size- the message size in bytesfactor- multiplier of size for the drop calculation, 1 for no adjustment- Returns:
- true for accepted, false for drop
getCurrentParticipatingBandwidth
int getCurrentParticipatingBandwidth()Out bandwidth. Actual bandwidth, not smoothed, not bucketed.- Returns:
- Bps in recent period (a few seconds)
getCurrentParticipatingBandwidthIn
int getCurrentParticipatingBandwidthIn()In bandwidth. Actual bandwidth, not smoothed, not bucketed.- Returns:
- Bps in recent period (a few seconds)
updateParticipating
private void updateParticipating(long now) Call a few times a minute to update the stats.- Parameters:
now- the current time in ms; currently unused