RateLimitingAlgorithm

sealed class RateLimitingAlgorithm(val totalPermits: Int, val replenishmentPeriod: Duration, val queueLength: Int)(source)

Represents all possible rate limiting algorithms that can be used to determine the behaviour of a RateLimiter.

See also

Inheritors

Constructors

Link copied to clipboard
protected constructor(totalPermits: Int, replenishmentPeriod: Duration, queueLength: Int)

Types

Link copied to clipboard
data class FixedWindowCounter(val totalPermits: Int, val replenishmentPeriod: Duration, val queueLength: Int) : RateLimitingAlgorithm

Represents a rate limiting algorithm that will allow a fixed number of permits in a given time period (e.g., 5 requests per minute). At the beginning of each time window, the counter is reset, and requests are counted from that point.

Link copied to clipboard
data class SlidingWindowCounter(val totalPermits: Int, val replenishmentPeriod: Duration, val segments: Int, val queueLength: Int) : RateLimitingAlgorithm

Represents a rate limiting algorithm that divides the total time period into smaller segments or windows. Each segment has its own counter, and the rate limiter keeps track of the number of requests in each segment to provide a more accurate and smoother rate limiting mechanism. After segments * replenishmentPeriod, which emcompasses a window cycle, the oldest segment is removed, and a new segment is added. At any point, when a request arrives, the total number of requests in all segments is compared to the totalPermits limit.

Link copied to clipboard
data class TokenBucket(val totalPermits: Int, val replenishmentPeriod: Duration, val queueLength: Int) : RateLimitingAlgorithm

Represents a rate limiting algorithm that has a fixed bucket size and refills the bucket with tokens at a constant rate. At each replenishmentPeriod, the bucket is refilled with 1 token (e.g., plus 1 token per second), never exceeding the totalPermits limit.

Properties

Link copied to clipboard
open val queueLength: Int

The maximum number of requests that can be queued when the rate limiter is exceeded. If set to 0, the rate limiter will reject requests immediately when the limit is reached.

Link copied to clipboard

The amount of time that must pass before the rate limiting algorithm replenishes permits. Depends on the algorithm.

Link copied to clipboard
open val totalPermits: Int

The total number of permits that can be allowed in a given replenishmentPeriod.