Skip to main content
Deno 2 is finally here 🎉️
Learn more
Extremely Popular
Go to Latest
interface LeakyBucket
Re-export
import { type LeakyBucket } from "https://deno.land/x/discordeno@17.2.0/template/beginner/deps.ts";

A Leaky Bucket. Useful for rate limiting purposes. This uses performance.now() instead of Date.now() for higher accuracy.

NOTE: This bucket is lazy, means it only updates when a related method is called.

Properties

max: number

How many tokens this bucket can hold.

refillAmount: number

Amount of tokens gained per interval. If bigger than max it will be pressed to max.

refillInterval: number

Interval at which the bucket gains tokens.

lastRefill: number
allowAcquire: boolean
tokensState: number
waiting: ((_?: unknown) => void)[]

Methods

acquire(amount: number, highPriority?: boolean): Promise<void>

Acquire tokens from the bucket. Resolves when the tokens are acquired and available.

nextRefill(): number

Returns the number of milliseconds until the next refill.

tokens(): number

Current tokens in the bucket.