api-rate-limiter-sdk
v0.0.2
Published
An npm package for API rate limiting with leaky bucket and token bucket algorithms.
Downloads
9
Readme
API Rate Limiter
A TypeScript library for API rate limiting with support for leaky bucket and token bucket algorithms. This package helps manage API request rates, implement retries, and apply backoff strategies.
Features
- Leaky Bucket Algorithm: Regulates the rate at which requests are processed.
- Token Bucket Algorithm: Allows bursts of requests while maintaining an average rate.
- Retries and Backoff: Handles request retries with configurable backoff strategies.
Installation
To install the package, use npm:
npm install api-rate-limiter-sdk
Or with yarn:
yarn add api-rate-limiter-sdk
Usage
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));
Token Bucket
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));
Configuration
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.
Running Tests
To run the tests for this package, use:
npm test
Acknowledgements
- ts-jest for TypeScript support in Jest.
- TypeScript for static typing.