Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ async function fastifyRateLimit (fastify, settings) {
pluginComponent.store = new Store(globalParams)
} else {
if (settings.redis) {
pluginComponent.store = new RedisStore(settings.redis, globalParams.timeWindow, settings.continueExceeding, settings.nameSpace || 'fastify-rate-limit-')
pluginComponent.store = new RedisStore(settings.redis, settings.nameSpace, globalParams.timeWindow, globalParams.continueExceeding)
} else {
pluginComponent.store = new LocalStore(settings.cache, globalParams.timeWindow, settings.continueExceeding)
pluginComponent.store = new LocalStore(settings.cache, globalParams.timeWindow, globalParams.continueExceeding)
}
}

Expand Down
6 changes: 3 additions & 3 deletions store/RedisStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ const lua = `
return {current, ttl}
`

function RedisStore (redis, timeWindow, continueExceeding, key) {
function RedisStore (redis, key = 'fastify-rate-limit-', timeWindow, continueExceeding) {
this.redis = redis
this.key = key
this.timeWindow = timeWindow
this.continueExceeding = continueExceeding
this.key = key

if (!this.redis.rateLimit) {
this.redis.defineCommand('rateLimit', {
Expand All @@ -46,7 +46,7 @@ RedisStore.prototype.incr = function (ip, cb, max) {
}

RedisStore.prototype.child = function (routeOptions) {
return new RedisStore(this.redis, routeOptions.timeWindow, routeOptions.continueExceeding, this.key + routeOptions.routeInfo.method + routeOptions.routeInfo.url + '-')
return new RedisStore(this.redis, `${this.key}${routeOptions.routeInfo.method}${routeOptions.routeInfo.url}-`, routeOptions.timeWindow, routeOptions.continueExceeding)
}

module.exports = RedisStore