TokenBucket

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

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.

Considerations

The token bucket algorithm is more flexible than the fixed window counter algorithm because it allows for bursts of requests up to the bucket size.

Parameters

replenishmentPeriod

The time period that the rate limiter will use to refill the bucket with 1 more token.

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.