Skip to content

Commit ae20d04

Browse files
authored
Pass the Redis database number as an option rather than in the URL (#17194)
* Pass the Redis database number as an option rather than in the URL * Remove rogue 'console.log'
1 parent ef387e4 commit ae20d04

2 files changed

Lines changed: 6 additions & 5 deletions

File tree

lib/redis-accessor.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const useRealRedis = !CI && NODE_ENV !== 'test' && !!REDIS_URL
1010
const redisMaxDb = REDIS_MAX_DB || 15
1111

1212
// Enable better stack traces in non-production environments
13-
const redisOptions = {
13+
const redisBaseOptions = {
1414
showFriendlyErrorStack: NODE_ENV !== 'production'
1515
}
1616

@@ -22,8 +22,10 @@ class RedisAccessor {
2222
)
2323
}
2424

25-
const redisUrl = `${REDIS_URL}/${databaseNumber}`
26-
const redisClient = useRealRedis ? new Redis(redisUrl, redisOptions) : new InMemoryRedis()
25+
const redisClient = useRealRedis
26+
? new Redis(REDIS_URL, { ...redisBaseOptions, db: databaseNumber })
27+
: new InMemoryRedis()
28+
2729
this._client = redisClient
2830

2931
this._prefix = prefix ? prefix.replace(/:+$/, '') + ':' : ''

middleware/rate-limit.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const Redis = require('ioredis')
55
const isProduction = process.env.NODE_ENV === 'production'
66
const { REDIS_URL } = process.env
77
const rateLimitDatabaseNumber = 0
8-
const redisUrl = `${REDIS_URL}/${rateLimitDatabaseNumber}`
98

109
module.exports = rateLimit({
1110
// 1 minute (or practically unlimited outside of production)
@@ -16,5 +15,5 @@ module.exports = rateLimit({
1615
// Or anything with a status code less than 400
1716
skipSuccessfulRequests: true,
1817
// When available, use Redis
19-
store: REDIS_URL && new RedisStore({ client: new Redis(redisUrl) })
18+
store: REDIS_URL && new RedisStore({ client: new Redis(REDIS_URL, { db: rateLimitDatabaseNumber }) })
2019
})

0 commit comments

Comments
 (0)