Package com.oorian.security
Class RateLimiter
java.lang.Object
com.oorian.security.RateLimiter
A fixed-window rate limiter for throttling requests per session.
Tracks the number of requests within a configurable time window and rejects requests that exceed the configured maximum. When the window expires, the counter resets automatically.
This class is thread-safe. All mutating operations are synchronized.
Usage:
RateLimiter limiter = new RateLimiter(100, 60000); // 100 requests per minute
if (limiter.tryAcquire())
{
// Process the request
}
else
{
// Reject: rate limit exceeded
}
- Since:
- 2.1
- Version:
- 1.0
- Author:
- Marvin P. Warble Jr.
-
Constructor Summary
ConstructorsConstructorDescriptionRateLimiter(int maxRequests, long windowMillis) Creates a new RateLimiter with the specified limits. -
Method Summary
Modifier and TypeMethodDescriptionintReturns the number of remaining requests allowed in the current window.voidreset()Resets the rate limiter, clearing the current window and count.booleanAttempts to acquire a permit for one request.
-
Constructor Details
-
RateLimiter
public RateLimiter(int maxRequests, long windowMillis) Creates a new RateLimiter with the specified limits.- Parameters:
maxRequests- the maximum number of requests allowed per windowwindowMillis- the time window duration in milliseconds
-
-
Method Details
-
tryAcquire
public boolean tryAcquire()Attempts to acquire a permit for one request.If the current time window has expired, the counter resets before checking. Returns
trueif the request is within the rate limit,falseif the limit has been exceeded.- Returns:
trueif the request is allowed,falseif rate limited
-
getRemainingRequests
public int getRemainingRequests()Returns the number of remaining requests allowed in the current window.- Returns:
- the number of remaining requests, or 0 if the limit has been reached
-
reset
public void reset()Resets the rate limiter, clearing the current window and count.
-