Class PerClientRateLimiter
java.lang.Object
com.loomcache.server.ratelimit.PerClientRateLimiter
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordAggregate statistics across all clients.static final recordConfiguration for per-client rate limiters. -
Constructor Summary
ConstructorsConstructorDescriptionInitialize per-client rate limiter. -
Method Summary
Modifier and TypeMethodDescriptionvoidBlocking acquisition of permit for a specific client.booleanBlocking acquisition with timeout for a specific client.Get aggregate statistics across all clients.@Nullable RateLimitergetClientLimiter(String clientId) Get the rate limiter for a specific client (does not create if doesn't exist).getClientStatistics(String clientId) Get statistics for a specific client.intGet the number of clients currently being tracked.booleanCheck if a specific client has a rate limiter.voidresetAll()Reset all client limiters and statistics.voidresetClient(String clientId) Reset the rate limiter for a specific client, removing its tracked state to free the client slot for reuse by other clients.booleantryAcquire(String clientId) Attempt to acquire a permit for a specific client.booleantryAcquire(String clientId, int permits) Attempt to acquire multiple permits for a specific client.
-
Constructor Details
-
PerClientRateLimiter
Initialize per-client rate limiter.- Parameters:
config- the configuration (must not be null)
-
-
Method Details
-
tryAcquire
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
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
Blocking acquisition of permit for a specific client.- Parameters:
clientId- the client identifier (must not be null or empty)
-
acquire
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 acquiretimeout- the timeout duration- Returns:
- true if permits were acquired, false if timeout occurred
-
getClientLimiter
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
Get statistics for a specific client.- Parameters:
clientId- the client identifier (must not be null)- Returns:
- statistics for the client
-
getAggregateStatistics
Get aggregate statistics across all clients. -
resetAll
public void resetAll()Reset all client limiters and statistics. -
resetClient
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
Check if a specific client has a rate limiter.
-