Skip to content

bottleneck原理

bottleneck 是一个用于 Node.js 的高效速率限制(Rate Limiting)库。它通过创建一个限制器(Limiter)来控制并发执行的任务数量和任务执行的速率。Bottleneck 的核心机制是将任务放入队列,根据设定的限制条件(如最大并发数和每单位时间允许的任务数)逐个执行任务。

重点

放入队列的任务带有resolve上下文,当resolve被执行后,可以继续后面的任务

当前bottlenck已经不维护了,使用起来问题特别多,很消耗内存, 考虑将其替换, 下面是一个简易版的 bottleneck

js
class Throttle {
  constructor() {
    this.queue = [];
    this.isConsume = false;
  }
  execTask(task, ...args) {
    return new Promise((resolve, reject) => {
      const executeTask = () => {
        task
          .apply(task, args)
          .then((result) => {
            resolve(result);
          })
          .catch((err) => {
            reject(err);
          });
      };
      this.queue.push(executeTask);
      this.consome();
    });
  }
  consome() {
    if (this.isConsume) {
      return;
    }
    this.isConsume = true;
    const interval = setInterval(() => {
      // console.log(this.queue.length, 'length');
      if (this.queue.length === 0) {
        clearInterval(interval);
        this.isConsume = false;
        return;
      }
      const nextTask = this.queue.shift();
      nextTask();
    }, 1000);
  }
}

const bottleneck = new Throttle();

const a = async () => {
  console.log("a");
};
const b = async (...x) => {
  console.log("b", x, new Date());
};
const c = async () => {
  console.log("c");
};

const main = async () => {
  await a();
  console.log(new Date());
  await bottleneck.execTask(b, "5555", new Date());
  console.log(new Date());
  await bottleneck.execTask(b, "6666", new Date());
  console.log(new Date());
  await bottleneck.execTask(b, "7777", new Date());
  console.log(new Date());
  await c();
};

main();

输出

js
a
2024-06-19T11:50:23.671Z
b [ '5555', 2024-06-19T11:50:23.671Z ] 2024-06-19T11:50:24.675Z
2024-06-19T11:50:24.677Z
b [ '6666', 2024-06-19T11:50:24.677Z ] 2024-06-19T11:50:25.675Z
2024-06-19T11:50:25.676Z
b [ '7777', 2024-06-19T11:50:25.676Z ] 2024-06-19T11:50:26.676Z
2024-06-19T11:50:26.678Z
c

Reference