-
-
Notifications
You must be signed in to change notification settings - Fork 73
feat: node-redis #413
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
feat: node-redis #413
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
'use strict' | ||
|
||
/** | ||
* When using node-redis, you need to initialize the client with the rateLimit script like this: | ||
* ```js | ||
* const redis = createClient({ | ||
* scripts: { | ||
* rateLimit: rateLimit.NodeRedisStore.rateLimitScript | ||
* } | ||
* }); | ||
* ``` | ||
*/ | ||
|
||
const { lua } = require('./RedisStore') | ||
const { defineScript } = require('@redis/client') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would not add a depedency to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah same as #413 (comment) The goal is to not depend on the lib Only the client that's passed should be used |
||
|
||
const rateLimitScript = defineScript({ | ||
NUMBER_OF_KEYS: 1, | ||
SCRIPT: lua, | ||
transformArguments (key, timeWindow, max, continueExceeding, exponentialBackoff) { | ||
return [key, String(timeWindow), String(max), String(continueExceeding), String(exponentialBackoff)] | ||
}, | ||
transformReply (reply) { | ||
return reply | ||
}, | ||
}) | ||
|
||
function NodeRedisStore (continueExceeding, exponentialBackoff, redis, key = 'fastify-rate-limit-') { | ||
this.continueExceeding = continueExceeding | ||
this.exponentialBackoff = exponentialBackoff | ||
this.redis = redis | ||
this.key = key | ||
alesblaznik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (!this.redis.rateLimit) { | ||
throw new Error( | ||
'rateLimit script missing on Redis instance. Add it when creating client: ' + | ||
'const redis = createClient({ scripts: { rateLimit: rateLimit.NodeRedisStore.rateLimitScript }})' | ||
) | ||
} | ||
} | ||
Comment on lines
+34
to
+40
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My only gripe with this is that we now expose the name of the script while with ioredis we'd load it internally. While the idea of injecting your own script seems cool, it currently seems a little inconsistent to me Can't we initialize or use the script after the client is created?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree it's not consistent. I've not seen this Thanks for the reference 🙏🏻 |
||
|
||
NodeRedisStore.prototype.incr = function (ip, cb, timeWindow, max) { | ||
this | ||
.redis | ||
.rateLimit(this.key + ip, timeWindow, max, this.continueExceeding, this.exponentialBackoff) | ||
.then(result => { | ||
cb(null, { current: result[0], ttl: result[1] }) | ||
}) | ||
.catch(err => { | ||
cb(err, null) | ||
}) | ||
} | ||
|
||
NodeRedisStore.prototype.child = function (routeOptions) { | ||
return new NodeRedisStore(routeOptions.continueExceeding, routeOptions.exponentialBackoff, this.redis, `${this.key}${routeOptions.routeInfo.method}${routeOptions.routeInfo.url}-`) | ||
} | ||
|
||
module.exports = NodeRedisStore | ||
module.exports.rateLimitScript = rateLimitScript |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,3 +62,4 @@ RedisStore.prototype.child = function (routeOptions) { | |
} | ||
|
||
module.exports = RedisStore | ||
module.exports.lua = lua | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need Should we move lua script to other place instead? |
Uh oh!
There was an error while loading. Please reload this page.