fastify-rate-limit icon indicating copy to clipboard operation
fastify-rate-limit copied to clipboard

🐛 Bug Report # Redis expire not working

Open aligzl opened this issue 5 years ago • 5 comments

I setup redis on rate-limit options, but it does not sets TTL ( expire )

  • node version: 16
  • fastify version: >=3.0.0
  • os: Mac

aligzl avatar May 03 '21 23:05 aligzl

Can you add an example to reproduce?

mcollina avatar May 03 '21 23:05 mcollina

It creates key in redis without TTL ( -1 ), so it doesnt expire and blocks permanently ( see image link below)

const fastify = require('fastify')({
  logger: {
    level: 'info' // Will use pino.destination()
  }
});
const crypto                = require('crypto');
const redis = require('./init-redis')
fastify.register(require('fastify-rate-limit'), {
  /* ... */
  
  keyGenerator: function(req) {
    return req.user ? req.user.username : crypto.createHash('md5').update(req.headers['ip'] || 'unknown' + req.headers['user-agent'] || 'unknown').digest('hex')
  },
  skipOnError: true,
  redis,
  global: true, max: 100, timeWindow: '1 minute'
})

https://imgur.com/gUuWgmH

aligzl avatar May 03 '21 23:05 aligzl

As you can see in https://github.com/fastify/fastify-rate-limit/blob/master/store/RedisStore.js, we are calling pexpire.

Please include a runnable example to reproduce your problem (something I can run with node file.js). I'd also be very happy if you could send a PR fixing this.

mcollina avatar May 04 '21 07:05 mcollina

@aligzl I experienced the same thing and it worked for me after I passed the time in ms as an integer instead of a string.

tabz24 avatar Sep 17 '21 14:09 tabz24

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

github-actions[bot] avatar Sep 01 '26 04:09 github-actions[bot]