Skip to content

Commit

Permalink
Use Redis Store for rate limiting when available (github#16484)
Browse files Browse the repository at this point in the history
  • Loading branch information
heiskr authored Nov 16, 2020
1 parent 9968098 commit 4d2e25b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 8 deletions.
8 changes: 6 additions & 2 deletions middleware/rate-limit.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
const rateLimit = require('express-rate-limit')
const RedisStore = require('rate-limit-redis')

const isProduction = process.env.NODE_ENV === 'production'
const REDIS_URL = process.env.REDIS_URL

module.exports = rateLimit({
// 1 minute (or practically unlimited outside of production)
windowMs: isProduction ? (60 * 1000) : 1,
// limit each IP to 20 requests per windowMs
// limit each IP to X requests per windowMs
max: 250,
// Don't rate limit requests for 200s and redirects
// Or anything with a status code less than 400
skipSuccessfulRequests: true
skipSuccessfulRequests: true,
// When available, use Redis
store: REDIS_URL && new RedisStore({ redisURL: REDIS_URL })
})
68 changes: 62 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"platform-utils": "^1.2.0",
"port-used": "^2.0.8",
"querystring": "^0.2.0",
"rate-limit-redis": "^2.0.0",
"readline-sync": "^1.4.10",
"resolve-url-loader": "^3.1.2",
"rimraf": "^3.0.0",
Expand Down

0 comments on commit 4d2e25b

Please sign in to comment.