SlidingWindowRateLimiter

Sliding window rate limiter implementation.

Unlike token bucket, this tracks requests within a time window and rejects requests if the limit is exceeded within that window.

Use Cases:

  • Strict rate limiting (e.g., "max 100 requests per minute")

  • Time-based quotas

  • More predictable rate limiting than token bucket

Parameters

config

Configuration for sliding window rate limiter

Example usage:

val rateLimiter = SlidingWindowRateLimiter.create(
config = SlidingWindowConfig(
maxRequests = 100,
windowDuration = 1.minutes
)
)

Types

Link copied to clipboard

Functions

Link copied to clipboard
suspend fun currentRequests(): Int

Gets the current number of requests in the window.

Link copied to clipboard
suspend fun <T> execute(block: suspend () -> T): T

Executes an operation with sliding window rate limiting.

Link copied to clipboard
suspend fun reset()

Resets the rate limiter to initial state.

Link copied to clipboard

Gets the current statistics.

Link copied to clipboard
suspend fun tryAcquire(): Boolean

Tries to acquire a permit within the sliding window.

Link copied to clipboard
suspend fun <T> tryExecute(block: suspend () -> T): T?

Tries to execute an operation, returning null if rate limited.

Inherited functions

Link copied to clipboard