-
-
Notifications
You must be signed in to change notification settings - Fork 34
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
RedisStore: args.length: Cannot read properties of undefined (reading length). #207
Comments
I have to mention: |
Hi @Gandalf1783, Apologies for the late reply. Looking through the source code for the It asks for two additional arguments: Thus, the final code should look something like this: // [ ... ]
const apiLimiter = rateLimit({
windowMs: 1 * 60 * 1000, // 1 minute
max: 10, // Limit each IP to 50 requests per `window` (here, per 1 minute)
standardHeaders: false, // Return rate limit info in the `RateLimit-*` headers
legacyHeaders: false, // Disable the `X-RateLimit-*` headers
store: new RedisStore({
sendCommand: (...args) => cluster.sendCommand(undefined, false, args),
}),
}); Hope this helps. |
Please double-check this @gamemaker1 & @Gandalf1783 Fixes #207 (hopefully)
Hello gamemaker! Thank you for your reply and for looking this up. In afterthought, I could've found this myself. Due to being sick, I might be able to test it in the next few days, if at all. However, I think this solves the issue. Thank you again and you will hear from me soon! |
Happy to help :) I am doubtful about whether passing Please do take care of your health first, and let me know what works once you get the time. |
Soo, the results are in! I will now try what I can do for firstKey, maybe this has something to do with it (being undefined)? |
Ah that's most likely because it's storing the script on initialisation in one server and calling it later on another server :/
Yea maybe Maybe cluster support is not really feasible the way the library is currently written. As a workaround, you could define a new redis client with only one of the servers and use that for rate limiting. |
It looks like node-redis has a way to define scripts when calling |
I think the real solution is probably for rate-limit-redis to include a default redis client, make sure it supports clustering, and just keep the old |
I wanted to comment on this issue, seeing as I ran into virtually the same problem as @Gandalf1783 , only a month after. I also followed: @gamemaker1 's suggestion and used Their other comment also helped me: #207 (comment) I tried to test the "basic" functionality of the library, that is to keep track of total hits for incloming ip addresses and maintain their ttl's. Please take this workaround with a grain of salt... Changed the sendCommand: (...args) => {
const commandName = args[0]
if (commandName === 'SCRIPT') {
let sendCommandResult
for (let i = 0; i < redisClient.masters.length; i++) {
const result = redisClient.masters[i].client?.sendCommand(args)
if (i === 0) {
sendCommandResult = result
}
}
return sendCommandResult
}
const firstArg = commandName === 'EVALSHA' ? args[3] : args[1]
return redisClient.sendCommand(firstArg, false, args)
} I'm assuming this function is called with // Redis store configuration
store: new RedisStore({
sendCommand: (...args: string[]) => client.sendCommand(args),
}), I also hardcoded the second argument for the client's sendCommand to be false (it's a boolean and its name is apparently The downside of course is that if the cluster is ever scaled up to have more nodes, the server would have to be restarted in order for the constructor to be initialized and run the script on the new nodes as well. A full solution would have to account for this case as well. With regards to trying to fix this, as @nfriedly already pointed out, there is a way to load Lua scripts to the client if you are to go with maintaining a full client. For the time being, maybe this can be further tested and added to the documentation? Or perhaps support for |
Description
I cannot instantiate RedisStore objects due to a "undefined" problem.:
will return:
Library version
4.2.0
Node version
18.20.3
Typescript version (if you are using it)
No response
Module system
CommonJS
The text was updated successfully, but these errors were encountered: