KeyedRateLimiter
class KeyedRateLimiter<Key>(val config: RateLimiterConfig = defaultRateLimiterConfig(), val semaphoreStateFactory: () -> SemaphoreState = { InMemorySemaphoreState() }) : AutoCloseable(source)
Manages multiple RateLimiter instances based on a Key, allowing rate limiting based on different keys. This is useful when you want to rate limit different resources or operations separately.
+--------------------------------+
| +------+ +------+ |
| |-> | K1 | ---> | RL | |
| | +------+ +------+ |
| | | K2 | |
Request | | +------+ +------+ |
----------> + --|-> | K3 | ---> | RL | |
| | +------+ +------+ |
| |-> | K4 | ---> | RL | |
| | +------+ +------+ |
| |-> | K5 | |
| +------+ |
+--------------------------------+
Content copied to clipboard
Since each rate limiter can be used in distributed architectures, the semaphore state can be stored in a shared data store, such as a database, by implementing the SemaphoreState interface.
Parameters
Key
The type of key used to identify different rate limiters.
config
The configuration defining the behavior of each rate limiter.
semaphoreStateFactory
A factory function to create semaphore states for rate limiters. Defaults to creating an in-memory semaphore state for each rate limiter.
See also
Constructors
Link copied to clipboard
constructor(config: RateLimiterConfig = defaultRateLimiterConfig(), semaphoreStateFactory: () -> SemaphoreState = { InMemorySemaphoreState() })