java.lang.Object
net.i2p.router.transport.FIFOBandwidthRefiller
All Implemented Interfaces:
Runnable

public class FIFOBandwidthRefillerextends Object implements 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 Details

    • _log

      private final Log _log
      Logger for the bandwidth refiller.
    • _context

      private final RouterContext _context
      The router context.
    • _limiter

      private final FIFOBandwidthLimiter _limiter
      The limiter whose queues are refilled.
    • _partBWE

      private volatile SyntheticREDQueue _partBWE
      Queue estimating the outbound share.
    • _partBWEIn

      private volatile SyntheticREDQueue _partBWEIn
      Queue estimating the inbound share.
    • _inboundKBytesPerSecond

      private volatile int _inboundKBytesPerSecond
      How many KBps to allow.
    • _outboundKBytesPerSecond

      private volatile int _outboundKBytesPerSecond
      How many KBps to allow.
    • _inboundBurstKBytesPerSecond

      private volatile int _inboundBurstKBytesPerSecond
      How many KBps to allow during burst.
    • _outboundBurstKBytesPerSecond

      private volatile int _outboundBurstKBytesPerSecond
      How many KBps to allow during burst.
    • _lastRefillTime

      private volatile long _lastRefillTime
      When we last replenished the queue.
    • _lastCheckConfigTime

      private volatile long _lastCheckConfigTime
      When we last checked the config for updates.
    • _configCheckPeriodMs

      private volatile long _configCheckPeriodMs
      How frequently we check the config for updates.
    • _isRunning

      private volatile boolean _isRunning
      True while the refiller runs.
    • PROP_INBOUND_BANDWIDTH

      public static final String PROP_INBOUND_BANDWIDTH
      Property for inbound bandwidth.
      See Also:
    • PROP_OUTBOUND_BANDWIDTH

      public static final String PROP_OUTBOUND_BANDWIDTH
      Property for outbound bandwidth.
      See Also:
    • PROP_INBOUND_BURST_BANDWIDTH

      public static final String PROP_INBOUND_BURST_BANDWIDTH
      Property for inbound burst bandwidth.
      See Also:
    • PROP_OUTBOUND_BURST_BANDWIDTH

      public static final String PROP_OUTBOUND_BURST_BANDWIDTH
      Property for outbound burst bandwidth.
      See Also:
    • PROP_INBOUND_BANDWIDTH_PEAK

      public static final String PROP_INBOUND_BANDWIDTH_PEAK
      Property for inbound bandwidth peak.
      See Also:
    • PROP_OUTBOUND_BANDWIDTH_PEAK

      public static final String PROP_OUTBOUND_BANDWIDTH_PEAK
      Property for outbound bandwidth peak.
      See Also:
    • DEFAULT_INBOUND_BANDWIDTH

      public static final int DEFAULT_INBOUND_BANDWIDTH
      Default inbound bandwidth, in KBps, unlimited is not allowed.
      See Also:
    • DEFAULT_OUTBOUND_BANDWIDTH

      public static final int DEFAULT_OUTBOUND_BANDWIDTH
      Caution, 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_BANDWIDTH
      Default inbound burst bandwidth.
      See Also:
    • DEFAULT_OUTBOUND_BURST_BANDWIDTH

      public static final int DEFAULT_OUTBOUND_BURST_BANDWIDTH
      Default outbound burst bandwidth.
      See Also:
    • DEFAULT_BURST_SECONDS

      public static final int DEFAULT_BURST_SECONDS
      Default burst seconds.
      See Also:
    • MIN_INBOUND_BANDWIDTH

      public static final int MIN_INBOUND_BANDWIDTH
      For 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_BANDWIDTH
      For 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_PEAK
      For 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_PEAK
      For 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_BANDWIDTH
      Max 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_PERCENTAGE
      Largest share of the capacity we take.
      See Also:
    • SHARE_LIMIT_FACTOR

      private static final float SHARE_LIMIT_FACTOR
      Share of the limit the refill stays under.
      See Also:
    • _replenishFrequency

      private static volatile long _replenishFrequency
      How 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 stats
      limiter - 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

      public void run()
      Specified by:
      run in interface Runnable
    • reinitialize

      void reinitialize()
      Reset refill timing and reload configuration.
    • updateQueues

      private boolean updateQueues(List<FIFOBandwidthLimiter.Request> buffer, long now)
      Refills the queues and reports any change.
      Parameters:
      buffer - the queues to refill
      now - the current time, in ms
      Returns:
      true if any limit changed
    • getShareBandwidth

      private int getShareBandwidth()
      In Bytes per second.
      Returns:
      the share bandwidth
    • 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 bytes
      factor - 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 bytes
      factor - 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)
    • getMaxShareBandwidth

      int getMaxShareBandwidth()
      Cap on bytes per second available to participating tunnels.
      Returns:
      the cap in Bps, or the outbound default before the queue exists
    • 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