net.i2p.router.transport

## Class FIFOBandwidthLimiter

- java.lang.Object
- - net.i2p.router.transport.FIFOBandwidthLimiter

-     public class FIFOBandwidthLimiter
      extends Object

  FIFO-based bandwidth limiter for managing inbound and outbound traffic. This class provides bandwidth management using First-In-First-Out queues for both inbound and outbound traffic. It implements token bucket algorithm to control data rates and prevent burst traffic patterns. Core Features:
  - FIFO request queuing for fair bandwidth allocation
  - Token bucket rate limiting
  - Separate inbound and outbound management
  - Configurable bandwidth limits and refill rates
  - Thread-safe operations with atomic counters
  - Request satisfaction and partial fulfillment handling
  Concurrency Strategy:
  - Java 5: Used synchronized ArrayList with head/tail access
  - Java 6+: Uses LinkedBlockingDeque for lock-free operations
  - Request polling from queue head for efficiency
  - Partial request satisfaction with push-back mechanism
  Algorithm:
  - Token refill at fixed intervals
  - Request processing when tokens available
  - Burst prevention through token depletion
  - Priority-based request handling

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static interface` | `FIFOBandwidthLimiter.CompleteListener` Listener for bandwidth request completion events.
    `static interface` | `FIFOBandwidthLimiter.Request` A bandwidth request, either inbound or outbound.

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `FIFOBandwidthLimiter(RouterContext context)`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `int` | `getCurrentParticipatingBandwidth()` Out bandwidth.
    `int` | `getInboundBurstBytes()`
    `int` | `getInboundBurstKBytesPerSecond()` The configured maximum, not the current rate.
    `int` | `getInboundKBytesPerSecond()` The configured maximum, not the current rate.
    `int` | `getMaxShareBandwidth()` In Bytes per second
    `int` | `getOutboundBurstBytes()`
    `int` | `getOutboundBurstKBytesPerSecond()` The configured maximum, not the current rate.
    `int` | `getOutboundKBytesPerSecond()` The configured maximum, not the current rate.
    `float` | `getReceiveBps()`
    `float` | `getReceiveBps15s()`
    `float` | `getSendBps()`
    `float` | `getSendBps15s()`
    `(package private) StringBuilder` | `getStatus()`
    `long` | `getTotalAllocatedInboundBytes()`
    `long` | `getTotalAllocatedOutboundBytes()`
    `long` | `now()`
    `(package private) void` | `refillBandwidthQueues(List<FIFOBandwidthLimiter.Request> buf, long bytesInbound, long bytesOutbound, long maxBurstIn, long maxBurstOut)` More bytes are available - add them to the queue and satisfy any requests we can
    `void` | `reinitialize()`
    `FIFOBandwidthLimiter.Request` | `requestInbound(int bytesIn, String purpose)` Request some bytes.
    `FIFOBandwidthLimiter.Request` | `requestOutbound(int bytesOut, int priority, String purpose)` Request some bytes.
    `boolean` | `sentParticipatingMessage(int size, float factor)` We intend to send traffic for a participating tunnel with the given size and adjustment factor.
    `(package private) void` | `setInboundBurstBytes(int bytes)`
    `(package private) void` | `setInboundBurstKBps(int kbytesPerSecond)`
    `(package private) void` | `setOutboundBurstBytes(int bytes)`
    `(package private) void` | `setOutboundBurstKBps(int kbytesPerSecond)`
    `void` | `shutdown()`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### FIFOBandwidthLimiter

          public FIFOBandwidthLimiter(RouterContext context)

  - ### Method Detail

    - #### getCurrentParticipatingBandwidth

          public int getCurrentParticipatingBandwidth()

      Out bandwidth. Actual bandwidth, not smoothed, not bucketed.
      Returns:
        Bps in recent period (a few seconds)

    - #### getInboundBurstBytes

          public int getInboundBurstBytes()

    - #### getInboundBurstKBytesPerSecond

          public int getInboundBurstKBytesPerSecond()

      The configured maximum, not the current rate. In binary K, i.e. rate / 1024.

    - #### getInboundKBytesPerSecond

          public int getInboundKBytesPerSecond()

      The configured maximum, not the current rate. In binary K, i.e. rate / 1024.

    - #### getMaxShareBandwidth

          public int getMaxShareBandwidth()

      In Bytes per second

    - #### getOutboundBurstBytes

          public int getOutboundBurstBytes()

    - #### getOutboundBurstKBytesPerSecond

          public int getOutboundBurstKBytesPerSecond()

      The configured maximum, not the current rate. In binary K, i.e. rate / 1024.

    - #### getOutboundKBytesPerSecond

          public int getOutboundKBytesPerSecond()

      The configured maximum, not the current rate. In binary K, i.e. rate / 1024.

    - #### getReceiveBps

          public float getReceiveBps()

      Returns:
        smoothed one second rate

    - #### getReceiveBps15s

          public float getReceiveBps15s()

      Returns:
        smoothed 15 second rate

    - #### getSendBps

          public float getSendBps()

      Returns:
        smoothed one second rate

    - #### getSendBps15s

          public float getSendBps15s()

      Returns:
        smoothed 15 second rate

    - #### getStatus

          StringBuilder getStatus()

    - #### getTotalAllocatedInboundBytes

          public long getTotalAllocatedInboundBytes()

    - #### getTotalAllocatedOutboundBytes

          public long getTotalAllocatedOutboundBytes()

    - #### now

          public long now()

    - #### refillBandwidthQueues

          final void refillBandwidthQueues(List<FIFOBandwidthLimiter.Request> buf,
                                           long bytesInbound,
                                           long bytesOutbound,
                                           long maxBurstIn,
                                           long maxBurstOut)

      More bytes are available - add them to the queue and satisfy any requests we can
      Parameters:
        `buf` - contains satisfied outbound requests, really just to avoid object thrash, not really used
        `maxBurstIn` - allow up to this many bytes in from the burst section for this time period (may be negative)
        `maxBurstOut` - allow up to this many bytes in from the burst section for this time period (may be negative)

    - #### reinitialize

          public void reinitialize()

    - #### requestInbound

          public FIFOBandwidthLimiter.Request requestInbound(int bytesIn,
                                                             String purpose)

      Request some bytes. Does not block.

    - #### requestOutbound

          public FIFOBandwidthLimiter.Request requestOutbound(int bytesOut,
                                                              int priority,
                                                              String purpose)

      Request some bytes. Does not block.

    - #### sentParticipatingMessage

          public boolean sentParticipatingMessage(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` - bytes
        `factor` - multiplier of size for the drop calculation, 1 for no adjustment
      Returns:
        true for accepted, false for drop

    - #### setInboundBurstBytes

          void setInboundBurstBytes(int bytes)

    - #### setInboundBurstKBps

          void setInboundBurstKBps(int kbytesPerSecond)

    - #### setOutboundBurstBytes

          void setOutboundBurstBytes(int bytes)

    - #### setOutboundBurstKBps

          void setOutboundBurstKBps(int kbytesPerSecond)

    - #### shutdown

          public void shutdown()