* Use [node-]redis as a direct dependency * Extract Redis client creation to its own module * Attach extensive logging in the Redis client creation module * Allow the rate limiter to pass requests when Redis is disconnected * Update rate-limit-redis * Default error input to empty object for formatRedisError method * Provide a name for the rate limiter's Redis client Co-authored-by: Kevin Heis <heiskr@users.noreply.github.com> Co-authored-by: Rachael Sewell <rachmari@github.com>
31 lines
1.2 KiB
JavaScript
31 lines
1.2 KiB
JavaScript
const rateLimit = require('express-rate-limit')
|
|
const RedisStore = require('rate-limit-redis')
|
|
const createRedisClient = require('../lib/redis/create-client')
|
|
|
|
const isProduction = process.env.NODE_ENV === 'production'
|
|
const { REDIS_URL } = process.env
|
|
const rateLimitDatabaseNumber = 0
|
|
const EXPIRES_IN_AS_SECONDS = 60
|
|
|
|
module.exports = rateLimit({
|
|
// 1 minute (or practically unlimited outside of production)
|
|
windowMs: isProduction ? (EXPIRES_IN_AS_SECONDS * 1000) : 1, // Non-Redis configuration in `ms`. Used as a fallback when Redis is not working or active.
|
|
// 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,
|
|
// When available, use Redis; if not, defaults to an in-memory store
|
|
store: REDIS_URL && new RedisStore({
|
|
client: createRedisClient({
|
|
url: REDIS_URL,
|
|
db: rateLimitDatabaseNumber,
|
|
name: 'rate-limit'
|
|
}),
|
|
// 1 minute (or practically unlimited outside of production)
|
|
expiry: isProduction ? EXPIRES_IN_AS_SECONDS : 1, // Redis configuration in `s`
|
|
// If Redis is not connected, let the request succeed as failover
|
|
passIfNotConnected: true
|
|
})
|
|
})
|