class ConnThrottler extends Object
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 and Description |
|---|
ConnThrottler(int max,
int totalMax,
long period,
long throttlePeriod,
long totalThrottlePeriod,
String action,
Log log) |
| 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.
|
public void clear()
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.
public boolean shouldThrottle(Hash h)
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:
h - the peer's destination hash to checkpublic void start()
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.
public void stop()
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.
public void updateLimits(int max,
int totalMax,
long checkPeriod,
long throttlePeriod,
long totalThrottlePeriod)
All period values are enforced with a minimum of 10 seconds.
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