FixedWindowCounter

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

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.

Considerations

It's important to note that the fixed window counter algorithm can lead to:

  • Bursts of requests around the boundary time of the fixed window, may result in strained resources as the window counter is reset in the middle of the traffic burst.

  • A stampeding effect, where previously rejected requests are retried simultaneously when the time window resets, causing spikes in traffic and overload the system, especially when dealing with a large number of clients.

Parameters

replenishmentPeriod

The time period that the rate limiter will use to reset the counter.

See also

Constructors

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

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