Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/RateLimiter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export type RateLimiterOpts = {
tokensPerInterval: number;
interval: Interval;
fireImmediately?: boolean;
bucketSize?: number;
};

/**
Expand All @@ -18,16 +19,18 @@ export type RateLimiterOpts = {
* one of the following strings: 'second', 'minute', 'hour', day'.
* @param options.fireImmediately Whether or not the promise will resolve
* immediately when rate limiting is in effect (default is false).
* @param options.bucketSize Maximum number of tokens to hold in the bucket.
* Also known as the burst rate. Defaults to options.tokensPerInterval.
*/
export class RateLimiter {
tokenBucket: TokenBucket;
curIntervalStart: number;
tokensThisInterval: number;
fireImmediately: boolean;

constructor({ tokensPerInterval, interval, fireImmediately }: RateLimiterOpts) {
constructor({ tokensPerInterval, interval, fireImmediately, bucketSize }: RateLimiterOpts) {
this.tokenBucket = new TokenBucket({
bucketSize: tokensPerInterval,
bucketSize: bucketSize ?? tokensPerInterval,
tokensPerInterval,
interval,
});
Expand Down