slow-it-down
Version:
A rate limiter for express using a token bucket
42 lines (41 loc) • 1.06 kB
TypeScript
export interface TokenBucketOptions {
/**
* Maximum number of tokens to hold in the bucket
*
* @type {number}
*/
capacity: number;
/**
* Number of tokens to fill the bucket over the course of an interval
*
* @type {number}
*/
fillRate: number;
}
/**
* Token Bucket Algorithm used in network throttling.
*
* See https://en.wikipedia.org/wiki/Token_bucket for more information
*/
export declare class TokenBucket {
capacity: number;
fillRate: number;
tokens: number;
private time;
constructor(options: TokenBucketOptions);
/**
* Consume N tokens from the bucket.
*
* If there is no capacity, the tokens are not pulled from the bucket.
*
* @param {Number} tokens the number of tokens to remove.
* @return {Boolean} True if there were enough tokens, otherwise false.
*/
consume(tokens: number): boolean;
/**
* Fills the bucket with more tokens.
*
* @return {Number} the current number of tokens in the bucket
*/
private fill();
}