Reputation: 11
I tried to use express rate limiter as follow:
require('express-rate-limit')({
windowMs: 24 * 60 * 60 * 1000, // 24 hrs in milliseconds
max: 5,
message: 'You have exceeded the 5 requests in 24 hrs limit!',
standardHeaders: true,
legacyHeaders: false,
})
It works fine but when i apply the cluster load balancer as like
if (cluster.isMaster) for (let i = 0; i < numCpus; i++) cluster.fork()
else shutdown(app.listen(PORT), mongoose)
It works fine unless the request gets handled by differnet process at any random moment.
It just doesn't work as expected. it's not blocking request based on the ip. Please help me or give me different solution for the rate limiter thanks
Upvotes: 1
Views: 548
Reputation: 8146
You should use the @express-rate-limit/cluster-memory-store to synchronize hit counts between the workers when using express-rate-limit with node.js's built-in cluster
module. (As long as you have only one server.)
Alternatively, any external store, such as the mongodb store, will also work to synchronize hits across your workers. This is the way to go if you have multiple servers.
Disclaimer: I'm the author of both express-rate-limit and @express-rate-limit/cluster-memory-store.
Upvotes: 0