Class RateLimit

java.lang.Object
net.targetr.wtm3.util.RateLimit

public class RateLimit extends Object
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

    Constructors
    Constructor
    Description
    RateLimit(int maxRequestsCount, long timeUnit)
    Constructs a new RateLimit instance with customizable rate limit values.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Checks if the given id has exceeded the allowed request rate limit.
    void
    Removes all request rate records that are older than the configured time unit.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 window
      timeUnit - the duration of the rate-limiting time window in milliseconds
  • Method Details

    • checkRequestRate

      public void checkRequestRate(String id) throws SecurityException
      Checks if the given id has exceeded the allowed request rate limit.

      If the limit is exceeded, a SecurityException is 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.