@koshnic/ratelimit
v1.0.3
Published
Redis based rate limiting implementation using the token bucket algorithm.
Downloads
291
Maintainers
Readme
Redis limiting for ioredis
Redis-based rate limiting using the token bucket algorithm.
This project is an adaptation for Node.js, inspired by go-redis.
Enjoy the benefits of the token bucket algorithm for both JavaScript and TypeScript developers.
Installation
npm install @koshnic/ratelimit
Usage
Note: All rate limit keys stored in Redis will be prefixed with rate_limit:
.
For example, when you invoke limiter.allowPerSecond('project:123', 10)
, the corresponding key name in Redis will be rate_limit:project:123
.
const Redis = require("ioredis");
const {RateLimiter} = require('@koshnic/ratelimit');
const redis = new Redis({
host: 'localhost',
port: 6379
});
const limiter = new RateLimiter(redis);
let res;
// Allow 10 requests per second
res = await limiter.allowPerSecond('project:123', 10);
// Allow 20 requests per minute
res = await limiter.allowPerMinute('project:456', 20);
// Allow 30 requests per hour
res = await limiter.allowPerHour('project:789', 30);
// Or you want to use raw allow function to meet your custom logic.
res = await limiter.allow('project:cutom_login', {
burst: 10,
ratePerPeriod: 10,
period: 60,
cost: 1
});
License
MIT