net.i2p.i2ptunnel

## Class ConnThrottler

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

-     class ConnThrottler
      extends 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.

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `ConnThrottler(int max, int totalMax, long period, long throttlePeriod, long totalThrottlePeriod, String action, Log log)`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `clear()` Resets all throttling state.
    `boolean` | `shouldThrottle(Hash h)` Checks if a peer or the total connection rate exceeds configured limits.
    `void` | `start()` Starts the throttler by scheduling the cleanup timer.
    `void` | `stop()` 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 java.lang.Object

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

- - ### Constructor Detail

    - #### ConnThrottler

          public ConnThrottler(int max,
                               int totalMax,
                               long period,
                               long throttlePeriod,
                               long totalThrottlePeriod,
                               String action,
                               Log log)

  - ### Method Detail

    - #### 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.

    - #### 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

    - #### 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