jhurliman / node-rate-limiter

A generic rate limiter for node.js. Useful for API clients, web crawling, or other tasks that need to be throttled
MIT License
1.51k stars 135 forks source link

RateLimiter slowing down with thousand of calls #85

Open pyrsmk opened 2 years ago

pyrsmk commented 2 years ago

I encounter an issue where when I spawn a whole bunch of limiter instances (several thousands). At first, the rate limit is pretty accurate, but at some point it begins to freeze every now and then. And it gets slower and slower.

Here's a clear example of what's going on:

const { RateLimiter } = require('limiter')
const limiter = new RateLimiter({ tokensPerInterval: 1, interval: 100 })

let index = 0
const newIndex = () => ++index;

[...Array(5000).keys()].forEach(async () => {
  const idx = newIndex()
  console.log(`[${idx}] awaiting`)
  await limiter.removeTokens(1)
  console.log(`[${idx}] delivered`)
})
sir-george2500 commented 1 month ago

did you check the amount of garbage collection that was taken place becase copying array in Javascript uses lot of garbage collection

pyrsmk commented 1 month ago

Well, it was 3 years ago... If I remember well I implemented my own solution so everything is optimized and runs accurately.