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 ClassesModifier and TypeClassDescriptionprivate classTimer clearing the old peer records.private static classKeep a list of seen times, and a ban-until time.Field Summary
FieldsModifier and TypeFieldDescriptionprivate final StringConfig prefix of the throttler settings.private longWindow the connection counts cover.private final SimpleTimer2.TimedEventTimer clearing the old records.private intConnections accepted in the current period.private booleanTrue while the throttler is running.private final LogLogger for the connection throttling.private intConnections allowed per peer in the period.private final Map<Hash, ConnThrottler.Record> Connection records, by peer hash.private longTime a peer over its limit is throttled.private intConnections allowed in total in the period.private longTime all peers are throttled when over the total.private longWhen the total throttle expires.Constructor Summary
ConstructorsConstructorDescriptionConnThrottler(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 MethodsModifier and TypeMethodDescriptionvoidclear()Resets all throttling state.booleanChecks if a peer or the total connection rate exceeds configured limits.voidstart()Starts the throttler by scheduling the cleanup timer.voidstop()Stops the throttler and resets all state.voidupdateLimits(int max, int totalMax, long checkPeriod, long throttlePeriod, long totalThrottlePeriod) Updates the rate limiting configuration.
Field Details
_max
private int _maxConnections allowed per peer in the period._totalMax
private int _totalMaxConnections allowed in total in the period._checkPeriod
private long _checkPeriodWindow the connection counts cover._throttlePeriod
private long _throttlePeriodTime a peer over its limit is throttled._totalThrottlePeriod
private long _totalThrottlePeriodTime all peers are throttled when over the total._currentTotal
private int _currentTotalConnections accepted in the current period._peers
Connection records, by peer hash._totalThrottleUntil
private long _totalThrottleUntilWhen the total throttle expires._action
Config prefix of the throttler settings._log
Logger for the connection throttling._cleaner
Timer clearing the old records._isRunning
private boolean _isRunningTrue 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 unlimitedtotalMax- for all peers, 0 for unlimitedperiod- check window (ms)throttlePeriod- how long to ban a peer (ms)totalThrottlePeriod- how long to ban all peers (ms)action- name to note in loglog- 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 millisecondsthrottlePeriod- how long to throttle individual peers, in millisecondstotalThrottlePeriod- how long to throttle all peers, in milliseconds
shouldThrottle
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.