An npm package for API rate limiting with leaky bucket and token bucket algorithms.
npm install api-rate-limiter-sdkbash
npm install api-rate-limiter-sdk
`
Or with yarn:
`
yarn add api-rate-limiter-sdk
`
$3
Leaky Bucket:
`
import { LeakyBucket } from 'api-rate-limiter-sdk';
const bucket = new LeakyBucket({
capacity: 10, // Maximum number of requests
leakRate: 1 // Leak rate per second
});
bucket.addRequest()
.then(() => console.log('Request processed'))
.catch((err) => console.error('Request rate limited:', err));
`
$3
`
import { TokenBucket } from 'api-rate-limiter-sdk';
const bucket = new TokenBucket({
capacity: 10, // Maximum number of tokens
refillRate: 1 // Tokens added per second
});
bucket.addRequest()
.then(() => console.log('Request processed'))
.catch((err) => console.error('Request rate limited:', err));
`
$3
- capacity: The maximum number of requests or tokens allowed.
- leakRate (Leaky Bucket): The rate at which requests are processed.
- refillRate (Token Bucket): The rate at which tokens are added to the bucket.
$3
To run the tests for this package, use:
`
npm test
``