java.lang.Object
net.i2p.router.tunnel.pool.RequestThrottler.BurstWindowCounter
Enclosing class:
RequestThrottler

static class RequestThrottler.BurstWindowCounterextends Object
Sliding window counter for burst detection. Tracks per-peer request counts using 1-second buckets in a 10-second window.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final int
    Buckets making up the window.
    private final long
    Length of one bucket.
    private final AtomicLong
    When the bucket maps were last pruned.
    private final Map<Hash, AtomicIntegerArray>
    Per-peer request counts by bucket.
    private final long
    Total length of the sliding window.
  • Constructor Summary

    Constructors
    Constructor
    Description
    BurstWindowCounter(long windowSizeMs, int bucketCount)
    Divide the window into equal time buckets.
  • Method Summary

    Private Methods
    Modifier and Type
    Method
    Description
    (package private) void
    cleanup(long now)
    Drop peers whose current and previous buckets are both zero.
    (package private) int
    Window total for the peer without recording a request.
    (package private) int
    Count in the current bucket only, not the whole window.
    private int
    getWindowSum(AtomicIntegerArray array, int currentBucket)
    Sums one peer's counts across the whole window.
    (package private) int
    Record one request from the peer and return the fresh window total.
    (package private) boolean
    isBursting(Hash h, int normalLimit)
    Whether the peer's window total exceeds a burst limit derived from its normal limit (clamped between the min and max).

    Methods inherited from class Object

    clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • windowSizeMs

      private final long windowSizeMs
      Total length of the sliding window.
    • bucketCount

      private final int bucketCount
      Buckets making up the window.
    • bucketSizeMs

      private final long bucketSizeMs
      Length of one bucket.
    • peerBuckets

      private final Map<Hash, AtomicIntegerArray> peerBuckets
      Per-peer request counts by bucket.
    • lastCleanupTime

      private final AtomicLong lastCleanupTime
      When the bucket maps were last pruned.
  • Constructor Details

    • BurstWindowCounter

      BurstWindowCounter(long windowSizeMs, int bucketCount)
      Divide the window into equal time buckets.
      Parameters:
      windowSizeMs - total window length in ms
      bucketCount - equal time buckets covering the window
  • Method Details

    • increment

      int increment(Hash h)
      Record one request from the peer and return the fresh window total. Sweeps aged-out peers when the last sweep is over a minute old.
      Parameters:
      h - the requesting peer's hash
      Returns:
      requests within the window, including this one
    • getCount

      int getCount(Hash h)
      Window total for the peer without recording a request.
      Parameters:
      h - the peer's hash
      Returns:
      requests within the window, 0 if never seen
    • getCurrentBucketCount

      int getCurrentBucketCount(Hash h)
      Count in the current bucket only, not the whole window.
      Parameters:
      h - the peer's hash
      Returns:
      this bucket's count, 0 if never seen
    • isBursting

      boolean isBursting(Hash h, int normalLimit)
      Whether the peer's window total exceeds a burst limit derived from its normal limit (clamped between the min and max).
      Parameters:
      h - the peer's hash
      normalLimit - the peer's normal request limit
      Returns:
      true if the window total is above the derived burst limit
    • getWindowSum

      private int getWindowSum(AtomicIntegerArray array, int currentBucket)
      Sums one peer's counts across the whole window.
      Parameters:
      array - the peer's bucket counts
      currentBucket - the bucket being written now
      Returns:
      the total requests in the window
    • cleanup

      void cleanup(long now)
      Drop peers whose current and previous buckets are both zero. CAS-claimed so concurrent callers mostly skip the sweep.
      Parameters:
      now - current time in ms