Node Aerospike Rate Limiter
NodeJS library for fixed window rate limiting using Aerospike as data store.
Features
- Uses Fixed window for rate limiting
- Multiple rules per RateLimiter instance
- Ability to create multiple RateLimiter instance
- Can be used through middleware as well
- Ability to pass custom methods for rate limiting - Defaults to IP based rate limiting
Install
npm install node-aerospike-rate-limiter --save
Examples
Basic Usage
var RateLimiter = RateLimiter;var Aerospike = ;const client = Aerospike; //create aerospike client /* Create an instance of RateLimiter, create multiple instance if required */var options = duration: 15 limit: 5 prefix: "SOME_KEY_PREFIX"; //duration in seconds, prefix is optional Eg: 5 requests in 15 secondsvar rateLimiter = client namespace //aerospike namespace rateLimitSet //aerospike set options; /* rate limit on any key. like email, ip etc */rateLimiter /* reset rate limit for the key Eg: if you are rate limiting on Login and want to reset after a successful login */ rateLimiter
Usage as Middleware
Rate Limit all the endpoints:
var RateLimiter = RateLimiter;var RateLimiterMiddleware = RateLimitMiddleware;var Aerospike = ;const client = Aerospike; //create aerospike client /* Create an instance of RateLimiter */var options = duration: 15 limit: 5 prefix: "SOME_KEY_PREFIX"; //duration in seconds, prefix is optionalvar rateLimiter = client namespace //aerospike namespace rateLimitSet //aerospike set options; var rateLimitMiddleware = rateLimiter; app;
Rate Limit specific endpoints:
var rateLimitMiddleware = rateLimiter; var limit = rateLimitMiddleware; app;
Rate Limiting on custom field
If you want to rate limit on custom fields, you need to pass it in options to the middleware. By default, it rate limits on user's IP
//Create a custom method, req should be passed to the methodvar { return reqheaders'x-app-id'; //return any custom string, will be used as key}; var limit = rateLimitMiddleware; app;
If you want to disable rate limiting for staging environment:
var options = keyExtractor: keyExtractor ignoreRateLimiting: true; //defaults to false var limit = rateLimitMiddleware;