Skip to main content
Version: 1.x

TokenBucketAlgorithm

TokenBucketAlgorithm

Token bucket algorithm for bursty traffic with steady refill.

Signature
class TokenBucketAlgorithm implements RateLimitAlgorithm {
public readonly type: RateLimitAlgorithmType = 'token-bucket';
constructor(storage: RateLimitStorage, config: TokenBucketConfig)
consume(key: string) => Promise<RateLimitResult>;
reset(key: string) => Promise<void>;
}

type

constructor

method
(storage: RateLimitStorage, config: TokenBucketConfig) => TokenBucketAlgorithm

Create a token-bucket algorithm bound to a storage backend.

consume

method
(key: string) => Promise<RateLimitResult>

Record one attempt and return the current bucket status for this key.

reset

method
(key: string) => Promise<void>

Reset the stored key state for this limiter.