limiter
- Version 2.1.0
- Published
- 221 kB
- 1 dependency
- MIT license
Install
npm i limiter
yarn add limiter
pnpm add limiter
Overview
A generic rate limiter for the web and node.js. Useful for API clients, web crawling, or other tasks that need to be throttled
Index
Classes
class RateLimiter
class RateLimiter {}
A generic rate limiter. Underneath the hood, this uses a token bucket plus an additional check to limit how many tokens we can remove each interval.
Parameter options
Parameter
options.tokensPerInterval Maximum number of tokens that can be removed at any given moment and over the course of one interval.
Parameter
options.interval The interval length in milliseconds, or as one of the following strings: 'second', 'minute', 'hour', day'.
Parameter
options.fireImmediately Whether or not the promise will resolve immediately when rate limiting is in effect (default is false).
constructor
constructor({ tokensPerInterval, interval, fireImmediately }: RateLimiterOpts);
property curIntervalStart
curIntervalStart: number;
property fireImmediately
fireImmediately: boolean;
property tokenBucket
tokenBucket: TokenBucket;
property tokensThisInterval
tokensThisInterval: number;
method getTokensRemaining
getTokensRemaining: () => number;
Returns the number of tokens remaining in the TokenBucket.
Returns
{Number} The number of tokens remaining.
method removeTokens
removeTokens: (count: number) => Promise<number>;
Remove the requested number of tokens. If the rate limiter contains enough tokens and we haven't spent too many tokens in this interval already, this will happen immediately. Otherwise, the removal will happen when enough tokens become available.
Parameter count
The number of tokens to remove.
Returns
A promise for the remainingTokens count.
method tryRemoveTokens
tryRemoveTokens: (count: number) => boolean;
Attempt to remove the requested number of tokens and return immediately. If the bucket (and any parent buckets) contains enough tokens and we haven't spent too many tokens in this interval already, this will return true. Otherwise, false is returned.
Parameter count
The number of tokens to remove.
Parameter True
if the tokens were successfully removed, otherwise false.
class TokenBucket
class TokenBucket {}
A hierarchical token bucket for rate limiting. See http://en.wikipedia.org/wiki/Token_bucket for more information.
Parameter options
Parameter
options.bucketSize Maximum number of tokens to hold in the bucket. Also known as the burst rate.
Parameter
options.tokensPerInterval Number of tokens to drip into the bucket over the course of one interval.
Parameter
options.interval The interval length in milliseconds, or as one of the following strings: 'second', 'minute', 'hour', day'.
Parameter
options.parentBucket Optional. A token bucket that will act as the parent of this bucket.
constructor
constructor({ bucketSize, tokensPerInterval, interval, parentBucket,}: TokenBucketOpts);
property bucketSize
bucketSize: number;
property content
content: number;
property interval
interval: number;
property lastDrip
lastDrip: number;
property parentBucket
parentBucket?: TokenBucket;
property tokensPerInterval
tokensPerInterval: number;
method drip
drip: () => boolean;
Add any new tokens to the bucket since the last drip.
Returns
{Boolean} True if new tokens were added, otherwise false.
method removeTokens
removeTokens: (count: number) => Promise<number>;
Remove the requested number of tokens. If the bucket (and any parent buckets) contains enough tokens this will happen immediately. Otherwise, the removal will happen when enough tokens become available.
Parameter count
The number of tokens to remove.
Returns
A promise for the remainingTokens count.
method tryRemoveTokens
tryRemoveTokens: (count: number) => boolean;
Attempt to remove the requested number of tokens and return immediately. If the bucket (and any parent buckets) contains enough tokens this will return true, otherwise false is returned.
Parameter count
The number of tokens to remove.
Parameter True
if the tokens were successfully removed, otherwise false.
Type Aliases
type Interval
type Interval = number | 'second' | 'sec' | 'minute' | 'min' | 'hour' | 'hr' | 'day';
type RateLimiterOpts
type RateLimiterOpts = { tokensPerInterval: number; interval: Interval; fireImmediately?: boolean;};
type TokenBucketOpts
type TokenBucketOpts = { bucketSize: number; tokensPerInterval: number; interval: Interval; parentBucket?: TokenBucket;};
Package Files (3)
Dependencies (1)
Dev Dependencies (15)
Peer Dependencies (0)
No peer dependencies.
Badge
To add a badge like this oneto your package's README, use the codes available below.
You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/limiter
.
- Markdown[![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/limiter)
- HTML<a href="https://www.jsdocs.io/package/limiter"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>
- Updated .
Package analyzed in 3478 ms. - Missing or incorrect documentation? Open an issue for this package.