SlidingWindowCounter

data class SlidingWindowCounter(val totalPermits: Int, val replenishmentPeriod: Duration, val segments: Int, val queueLength: Int) : RateLimitingAlgorithm(source)

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.

Considerations

This algorithm mitigates the burstiness at the boundary of fixed window implmentations by distributing requests more evenly across time segments (e.g., if the current window is 25% through, the previous window's count is weighted by 75%).

Parameters

replenishmentPeriod

The duration of each time segment.

segments

The number of segments that the time window is divided into.

See also

Constructors

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

Properties

Link copied to clipboard
open override 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
open override val replenishmentPeriod: Duration
Link copied to clipboard
Link copied to clipboard
open override val totalPermits: Int

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