Class PerClientRateLimiter

java.lang.Object
com.loomcache.server.ratelimit.PerClientRateLimiter

public class PerClientRateLimiter extends Object
Per-client/per-key rate limiter that maintains separate rate limiters for each client. Uses a ConcurrentHashMap for thread-safe access to individual client limiters. Provides centralized statistics across all clients.
  • Constructor Details

    • PerClientRateLimiter

      public PerClientRateLimiter(PerClientRateLimiter.PerClientConfig config)
      Initialize per-client rate limiter.
      Parameters:
      config - the configuration (must not be null)
  • Method Details

    • tryAcquire

      public boolean tryAcquire(String clientId)
      Attempt to acquire a permit for a specific client.
      Parameters:
      clientId - the client identifier (must not be null or empty)
      Returns:
      true if permit was acquired, false otherwise
    • tryAcquire

      public boolean tryAcquire(String clientId, int permits)
      Attempt to acquire multiple permits for a specific client.
      Parameters:
      clientId - the client identifier (must not be null or empty)
      permits - the number of permits to acquire (must be positive)
      Returns:
      true if all permits were acquired, false otherwise
      Throws:
      IllegalArgumentException - if clientId is invalid or permits invalid input: '<'= 0
    • acquire

      public void acquire(String clientId)
      Blocking acquisition of permit for a specific client.
      Parameters:
      clientId - the client identifier (must not be null or empty)
    • acquire

      public boolean acquire(String clientId, int permits, Duration timeout)
      Blocking acquisition with timeout for a specific client.
      Parameters:
      clientId - the client identifier (must not be null or empty)
      permits - the number of permits to acquire
      timeout - the timeout duration
      Returns:
      true if permits were acquired, false if timeout occurred
    • getClientLimiter

      public @Nullable RateLimiter getClientLimiter(String clientId)
      Get the rate limiter for a specific client (does not create if doesn't exist).
      Parameters:
      clientId - the client identifier (must not be null)
      Returns:
      the rate limiter, or null if not found
    • getClientStatistics

      public RateLimiter.RateLimiterStats getClientStatistics(String clientId)
      Get statistics for a specific client.
      Parameters:
      clientId - the client identifier (must not be null)
      Returns:
      statistics for the client
    • getAggregateStatistics

      public PerClientRateLimiter.AggregateStats getAggregateStatistics()
      Get aggregate statistics across all clients.
    • resetAll

      public void resetAll()
      Reset all client limiters and statistics.
    • resetClient

      public void resetClient(String clientId)
      Reset the rate limiter for a specific client, removing its tracked state to free the client slot for reuse by other clients.
    • getTrackedClientCount

      public int getTrackedClientCount()
      Get the number of clients currently being tracked.
    • hasClient

      public boolean hasClient(String clientId)
      Check if a specific client has a rate limiter.