Rate Limiter
Token bucket rate limiter for controlling request throughput.
The Rate Limiter pattern controls the rate of operations to prevent overwhelming a system or service. It uses a token bucket algorithm where tokens are added at a fixed rate, and operations consume tokens.
Use Cases:
API rate limiting
Prevent service overload
Comply with third-party API rate limits
Control resource consumption
Token Bucket Algorithm:
Tokens are added to a bucket at a fixed rate
Each operation consumes one or more tokens
If no tokens are available, the operation is throttled
Allows bursts up to bucket capacity
Basic usage:
val limiter = RateLimiter.create(config = RateLimiterConfig(
permitsPerSecond = 10.0,
burstCapacity = 20
))
val result = limiter.tryExecute {
callApi()
} // returns null if rate exceededBlocking execution:
val result = limiter.execute {
callApi()
} // waits for available tokenSliding window variant:
val limiter = SlidingWindowRateLimiter.create(config = SlidingWindowConfig(
maxRequests = 100,
windowDuration = 1.minutes
))Parameters
Configuration for rate limiter behavior
Types
Factory for RateLimiter.
Functions
Adds a listener for rate limiter events.
Gets the current number of available tokens.
Resets all statistics counters.
Gets the current statistics.
Tries to acquire the specified number of permits.
Tries to execute an operation, returning null if rate limited.
Inherited functions
Adapts this RateLimiter into a Policy.