可读流setInterval设置为1ms

时间:2019-07-09 19:23:26

标签: javascript node.js stream setinterval

我编写了这段代码,使我可以在Node.js中创建可读流,并尽可能频繁地将数据传出双端队列:

const limiter = new RateLimiter(count, interval)
const deque = new Deque()

const getStream = () => {
  const inStream = new Readable({
    objectMode: true,
    read() {}
  })

  setInterval(() => {
    if (deque.length >= count) {
      const result = limiter.tryRemoveTokens(count)
      if (result) {
        for (let i = 0; i < count; i++) {
          inStream.push(deque.shift())
        }
      }
    }
  }, 1)

  return inStream
}

每隔1毫秒执行一次setInterval是一种好习惯吗?

0 个答案:

没有答案