rate-limit-threshold
Installation
npm install rate-limit-threshold
This package is an ESM package. Your project needs to be ESM too. Read more.
Usage
A rate-limit-threshold which restricts a maximum number of call in a given time frame.
Example
import {RateLimitThreshold} from 'rate-limit-threshold';
(async () => {
const rateLimitThreshold = new RateLimitThreshold(3, 1); // Maximum 3 requests in 1 seconds
for (let n = 0; n < 7; ++n) {
const delayInMs = await rateLimitThreshold.limit(); // Slow down loop to comply with 20 call in 10 seconds
console.log('Timeout applied to coply with rate limit', delayInMs);
// after the limit() is been aplied, you call your limitted requested
}
})();
API
RateLimitThreshold
Constructor
new RateLimitThreshold(requests, period);
Arguments:
-
requests
, number, Allowed requests within period (window) -
period
, number, Period (window) in milliseconds
limit
Call limit before the function you want call no more than requests
in per period
.
const timeSleptInMs await = limit()
Promise which resolved when timeout (sleep). The time in milliseconds slept is returned.