java.lang.Object
net.i2p.i2ptunnel.ConnThrottler

class ConnThrottlerextends Object
Connection rate limiter providing basic DoS protection.

Counts events per peer and across all peers. Ban time differs from check time, with separate map of throttled peers and individual timestamps.

Differs from streaming version: more precise tracking vs. lightweight but "sloppy" single time bucket approach.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    private class 
    Timer clearing the old peer records.
    private static class 
    Keep a list of seen times, and a ban-until time.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final String
    Config prefix of the throttler settings.
    private long
    Window the connection counts cover.
    Timer clearing the old records.
    private int
    Connections accepted in the current period.
    private boolean
    True while the throttler is running.
    private final Log
    Logger for the connection throttling.
    private int
    Connections allowed per peer in the period.
    private final Map<Hash, ConnThrottler.Record>
    Connection records, by peer hash.
    private long
    Time a peer over its limit is throttled.
    private int
    Connections allowed in total in the period.
    private long
    Time all peers are throttled when over the total.
    private long
    When the total throttle expires.
  • Constructor Summary

    Constructors
    Constructor
    Description
    ConnThrottler(int max, int totalMax, long period, long throttlePeriod, long totalThrottlePeriod, String action, Log log)
    Applies the limits and starts the cleaner that expires throttled peers.
  • Method Summary

    All Methods
    Modifier and Type
    Method
    Description
    void
    Resets all throttling state.
    boolean
    Checks if a peer or the total connection rate exceeds configured limits.
    void
    Starts the throttler by scheduling the cleanup timer.
    void
    Stops the throttler and resets all state.
    void
    updateLimits(int max, int totalMax, long checkPeriod, long throttlePeriod, long totalThrottlePeriod)
    Updates the rate limiting configuration.

    Methods inherited from class Object

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

    • _max

      private int _max
      Connections allowed per peer in the period.
    • _totalMax

      private int _totalMax
      Connections allowed in total in the period.
    • _checkPeriod

      private long _checkPeriod
      Window the connection counts cover.
    • _throttlePeriod

      private long _throttlePeriod
      Time a peer over its limit is throttled.
    • _totalThrottlePeriod

      private long _totalThrottlePeriod
      Time all peers are throttled when over the total.
    • _currentTotal

      private int _currentTotal
      Connections accepted in the current period.
    • _peers

      private final Map<Hash, ConnThrottler.Record> _peers
      Connection records, by peer hash.
    • _totalThrottleUntil

      private long _totalThrottleUntil
      When the total throttle expires.
    • _action

      private final String _action
      Config prefix of the throttler settings.
    • _log

      private final Log _log
      Logger for the connection throttling.
    • _cleaner

      private final SimpleTimer2.TimedEvent _cleaner
      Timer clearing the old records.
    • _isRunning

      private boolean _isRunning
      True while the throttler is running.
  • Constructor Details

    • ConnThrottler

      public ConnThrottler(int max, int totalMax, long period, long throttlePeriod, long totalThrottlePeriod, String action, Log log)
      Applies the limits and starts the cleaner that expires throttled peers.
      Parameters:
      max - per-peer max, 0 for unlimited
      totalMax - for all peers, 0 for unlimited
      period - check window (ms)
      throttlePeriod - how long to ban a peer (ms)
      totalThrottlePeriod - how long to ban all peers (ms)
      action - name to note in log
      log - logger that logs the throttling action
  • Method Details

    • start

      public void start()
      Starts the throttler by scheduling the cleanup timer.

      This method must be called to begin rate limiting. The cleanup timer will run periodically to check for expired throttles and clean up stale peer records.

      If already started, this method has no effect.

    • stop

      public void stop()
      Stops the throttler and resets all state.

      This method cancels the cleanup timer, clears all peer records, and resets the total connection counter. The throttler may be restarted by calling start() again.

    • updateLimits

      public void updateLimits(int max, int totalMax, long checkPeriod, long throttlePeriod, long totalThrottlePeriod)
      Updates the rate limiting configuration.

      All period values are enforced with a minimum of 10 seconds.

      Parameters:
      max - the maximum number of connections per peer (0 for unlimited)
      totalMax - the maximum total connections from all peers (0 for unlimited)
      checkPeriod - the time window for counting connections, in milliseconds
      throttlePeriod - how long to throttle individual peers, in milliseconds
      totalThrottlePeriod - how long to throttle all peers, in milliseconds
    • shouldThrottle

      public boolean shouldThrottle(Hash h)
      Checks if a peer or the total connection rate exceeds configured limits.

      This method increments the connection counter for the given peer and checks if either the per-peer limit or the total connection limit has been exceeded. If a limit is exceeded, the peer (or all peers) is throttled for the configured throttle period.

      Throttle Logic:

      • Individual: If max connections exceeded in checkPeriod, throttle for throttlePeriod
      • Total: If total connections exceeded, throttle all for totalThrottlePeriod
      Parameters:
      h - the peer's destination hash to check
      Returns:
      true if the peer should be throttled (request denied), false otherwise
    • clear

      public void clear()
      Resets all throttling state.

      This method clears all peer records, resets the total connection counter, and clears the total throttle timer. After calling this method, all connections will be allowed until limits are exceeded again.