microlinkhq / async-ratelimiter

Rate limit made simple, easy, async.
MIT License
320 stars 23 forks source link

limiter never frees up #11

Closed rickyk586 closed 5 years ago

rickyk586 commented 5 years ago
const limiter = new RateLimiter({
    duration: 2000,
    max: 2,
    id: 'something',
    db: redisClient
});
setInterval(async () => {
    const r = await limiter.get();
    console.log(r.remaining);
}, 500);

logs 2, 1, 0, 0, 0, 0, 0...

I would expect it to free up a slot after 2 seconds and r.remaining to = 1. If I change the setInterval to 1000, it works as expected.

Kikobeats commented 5 years ago

I think this is tested here: https://github.com/microlinkhq/async-ratelimiter/blob/master/test/index.js#L339

rickyk586 commented 5 years ago

perhaps it makes a difference if it reaches 0

Kikobeats commented 5 years ago

The same is happening in the original library 🤔

'use strict'

const RateLimiter = require('ratelimiter')
//const RateLimiter = require('.')
const Redis = require('ioredis')
const { promisify } = require('util')

const limiter = new RateLimiter({
  duration: 2000,
  max: 2,
  id: 'something',
  db: new Redis()
})

const get = promisify(limiter.get.bind(limiter))

let index = -1
setInterval(async () => {
  console.log('---')
  console.log('iteration', ++index)
  const r = await get()
  console.log('value', r)
}, 100)

Can you open the issue there as well?