Skip to content

Commit 4d2e25b

Browse files
authored
Use Redis Store for rate limiting when available (#16484)
1 parent 9968098 commit 4d2e25b

3 files changed

Lines changed: 69 additions & 8 deletions

File tree

middleware/rate-limit.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
const rateLimit = require('express-rate-limit')
2+
const RedisStore = require('rate-limit-redis')
23

34
const isProduction = process.env.NODE_ENV === 'production'
5+
const REDIS_URL = process.env.REDIS_URL
46

57
module.exports = rateLimit({
68
// 1 minute (or practically unlimited outside of production)
79
windowMs: isProduction ? (60 * 1000) : 1,
8-
// limit each IP to 20 requests per windowMs
10+
// limit each IP to X requests per windowMs
911
max: 250,
1012
// Don't rate limit requests for 200s and redirects
1113
// Or anything with a status code less than 400
12-
skipSuccessfulRequests: true
14+
skipSuccessfulRequests: true,
15+
// When available, use Redis
16+
store: REDIS_URL && new RedisStore({ redisURL: REDIS_URL })
1317
})

package-lock.json

Lines changed: 62 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
"platform-utils": "^1.2.0",
6262
"port-used": "^2.0.8",
6363
"querystring": "^0.2.0",
64+
"rate-limit-redis": "^2.0.0",
6465
"readline-sync": "^1.4.10",
6566
"resolve-url-loader": "^3.1.2",
6667
"rimraf": "^3.0.0",

0 commit comments

Comments
 (0)