Skip to content

Commit 29be6be

Browse files
chiedoJamesMGreene
andauthored
Make rate limiter configuration verbose (#17184)
Make rate limiter configuration verbose to prevent confusion in the future. Co-authored-by: chiedo <chiedo@users.noreply.github.com> Co-authored-by: James M. Greene <JamesMGreene@github.com>
1 parent 9171d17 commit 29be6be

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

middleware/rate-limit.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,22 @@ const Redis = require('ioredis')
55
const isProduction = process.env.NODE_ENV === 'production'
66
const { REDIS_URL } = process.env
77
const rateLimitDatabaseNumber = 0
8+
const EXPIRES_IN_AS_SECONDS = 60
89

910
module.exports = rateLimit({
1011
// 1 minute (or practically unlimited outside of production)
11-
windowMs: isProduction ? (60 * 1000) : 1,
12+
windowMs: isProduction ? (EXPIRES_IN_AS_SECONDS * 1000) : 1, // Non-Redis configuration in `ms`. Used as a fallback when Redis is not working or active.
1213
// limit each IP to X requests per windowMs
1314
max: 250,
1415
// Don't rate limit requests for 200s and redirects
1516
// Or anything with a status code less than 400
1617
skipSuccessfulRequests: true,
1718
// When available, use Redis
18-
store: REDIS_URL && new RedisStore({ client: new Redis(REDIS_URL, { db: rateLimitDatabaseNumber }) })
19+
store: REDIS_URL && new RedisStore({
20+
client: new Redis(REDIS_URL, {
21+
db: rateLimitDatabaseNumber
22+
}),
23+
// 1 minute (or practically unlimited outside of production)
24+
expiry: isProduction ? EXPIRES_IN_AS_SECONDS : 1 // Redis configuration in `s`
25+
})
1926
})

0 commit comments

Comments
 (0)