Distributed Rate Limiter
class DistributedRateLimiter(store: SharedStateStore, key: String, maxRequests: Int, window: Duration, clock: Clock = Clock.System)(source)
Cross-instance fixed-window rate limiter whose counter lives in a SharedStateStore (e.g. a Redis-backed one), so every process sharing the store shares one budget of maxRequests per window.
val limiter = DistributedRateLimiter(redisStore, "api", maxRequests = 100, window = 1.minutes)
limiter.execute { callApi() } // throws RateLimitExceededException when over budgetContent copied to clipboard
Constructors
Link copied to clipboard
constructor(store: SharedStateStore, key: String, maxRequests: Int, window: Duration, clock: Clock = Clock.System)
Functions
Link copied to clipboard
Tries to take one permit. Returns true if granted.
Link copied to clipboard
Like execute but returns null when over budget.