Installations
npm install redis-rate-limiter
Releases
Unable to fetch releases
Developer
Tabcorp
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
No
Node Version
8.11.3
NPM Version
5.6.0
Statistics
56 Stars
110 Commits
24 Forks
70 Watching
20 Branches
9 Contributors
Updated on 23 Oct 2024
Bundle Size
296.78 kB
Minified
73.90 kB
Minified + Gzipped
Languages
JavaScript (100%)
Total Downloads
Cumulative downloads
Total Downloads
2,225,879
Last day
-5.2%
2,310
Compared to previous day
Last week
3.2%
16,703
Compared to previous week
Last month
-2%
70,228
Compared to previous month
Last year
94.5%
886,313
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
redis-rate-limiter
Rate-limit any operation, backed by Redis.
- Inspired by ratelimiter
- But uses a fixed-window algorithm
- Great performance (>10000 checks/sec on local redis)
- No race conditions
Very easy to plug into Express
or Restify
to rate limit your Node.js
API.
Usage
Step 1: create a Redis connection
1var redis = require('redis'); 2var client = redis.createClient(6379, 'localhost', {enable_offline_queue: false});
Step 2: create your rate limiter
1var rateLimiter = require('redis-rate-limiter'); 2var limit = rateLimiter.create({ 3 redis: client, 4 key: function(x) { return x.id }, 5 rate: '100/minute' 6});
And go
1limit(request, function(err, rate) { 2 if (err) { 3 console.warn('Rate limiting not available'); 4 } else { 5 console.log('Rate window: ' + rate.window); // 60 6 console.log('Rate limit: ' + rate.limit); // 100 7 console.log('Rate current: ' + rate.current); // 74 8 if (rate.over) { 9 console.error('Over the limit!'); 10 } 11 } 12});
Options
redis
A pre-created Redis client. Make sure offline queueing is disabled.
1var client = redis.createClient(6379, 'localhost', { 2 enable_offline_queue: false 3});
key
The key is how requests are grouped for rate-limiting. Typically, this would be a user ID, a type of operation.
You can also specify any custom function:
1// rate-limit each user separately 2key: function(x) { return x.user.id; } 3 4// rate limit per user and operation type 5key: function(x) { return x.user.id + ':' + x.operation; } 6 7// rate limit everyone in the same bucket 8key: function(x) { return 'single-bucket'; }
You can also use the built-in ip
shorthand, which gets the remote address from an HTTP request.
1key: 'ip'
window
This is the duration over which rate-limiting is applied, in seconds.
1// rate limit per minute
2window: 60
3
4// rate limit per hour
5window: 3600
Note that this is not a rolling window.
If you specify 10 requests / minute
, a user would be able
to execute 10 requests at 00:59
and another 10 at 01:01
.
Then they won't be able to make another request until 02:00
.
limit
This is the total number of requests a unique key
can make during the window
.
1limit: 100
rate
Rate is a shorthand notation to combine limit
and window
.
1rate: '10/second' 2rate: '100/minute' 3rate: '1000/hour'
Or the even shorter
1rate: '10/s' 2rate: '100/m' 3rate: '100/h'
Note: the rate is parsed ahead of time, so this notation doesn't affect performance.
HTTP middleware
This package contains a pre-built middleware, which takes the same options
1var rateLimiter = require('redis-rate-limiter'); 2 3var middleware = rateLimiter.middleware({ 4 redis: client, 5 key: 'ip', 6 rate: '100/minute' 7}); 8 9server.use(middleware);
It rejects any rate-limited requests with a status code of HTTP 429
,
and an empty body.
Note: if you want to rate limit several routes individually, don't forget to use the route name as part of the key
, for example using Restify:
1function ipAndRoute(req) { 2 return req.connection.remoteAddress + ':' + req.route.name; 3} 4 5server.get( 6 {name: 'routeA', path: '/a'}, 7 rateLimiter.middleware({redis: client, key: ipAndRoute, rate: '10/minute'}), 8 controllerA 9); 10 11server.get( 12 {name: 'routeB', path: '/b'}, 13 rateLimiter.middleware({redis: client, key: ipAndRoute, rate: '20/minute'}), 14 controllerB 15);
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
license file not detected
Details
- Warn: project does not have a license file
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 30 are checked with a SAST tool
Reason
25 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-fwr7-v2mv-hh25
- Warn: Project is vulnerable to: GHSA-qwcr-r2fm-qrc7
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-pxg6-pf52-xh8x
- Warn: Project is vulnerable to: GHSA-h452-7996-h45h
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-rv95-896h-c2vc
- Warn: Project is vulnerable to: GHSA-qw6h-vgh9-j6wx
- Warn: Project is vulnerable to: GHSA-2j2x-2gpw-g8fm
- Warn: Project is vulnerable to: GHSA-ww39-953v-wcq6
- Warn: Project is vulnerable to: GHSA-qqgx-2p2h-9c37
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-8hfj-j24r-96c4
- Warn: Project is vulnerable to: GHSA-wc69-rhjr-hc9g
- Warn: Project is vulnerable to: GHSA-9wv6-86v2-598j
- Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp
- Warn: Project is vulnerable to: GHSA-35q2-47q7-3pc3
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-m6fv-jmcg-4jfg
- Warn: Project is vulnerable to: GHSA-cm22-4g7w-348p
- Warn: Project is vulnerable to: GHSA-c4w7-xm78-47vh
Score
1.5
/10
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More