Package net.targetr.wtm3.util
Class RateLimit
java.lang.Object
net.targetr.wtm3.util.RateLimit
Counts requests per identifier in a window anchored at its first tracked request.
Expired counters are removed during request-triggered cleanup, checked about once
per second. Exceeding the configured count throws
SecurityException.-
Constructor Summary
ConstructorsConstructorDescriptionRateLimit(int maxRequestsCount, long timeUnit) Constructs a new RateLimit instance with customizable rate limit values. -
Method Summary
Modifier and TypeMethodDescriptionvoidChecks if the given id has exceeded the allowed request rate limit.voidRemoves all request rate records that are older than the configured time unit.
-
Constructor Details
-
RateLimit
public RateLimit(int maxRequestsCount, long timeUnit) Constructs a new RateLimit instance with customizable rate limit values.- Parameters:
maxRequestsCount- the maximum allowed requests within the configured time windowtimeUnit- the duration of the rate-limiting time window in milliseconds
-
-
Method Details
-
checkRequestRate
Checks if the given id has exceeded the allowed request rate limit.If the limit is exceeded, a
SecurityExceptionis thrown. This method also triggers periodic cleanup of expired rate records.- Parameters:
id- the id of the client making the request- Throws:
SecurityException- if the id has exceeded the maximum allowed requests in the configured time unit
-
clearExpiredRates
public void clearExpiredRates()Removes all request rate records that are older than the configured time unit.This method iterates through the currently tracked records and discards those whose initial request timestamp is older than the configured time unit threshold.
-