简体   繁体   中英

Javascript - in-process function executor with rate limiter?

Imagine that I have some function f() which should be executed constantly. But I want to set a rate limit for execution, something like "Execute function no more than 10 times in 1 minute". In a code view I think it can have the following interface:

(async () => {
  const f = async () => {};
  const executor = new Executor({ max: 1, duration: 10000 });

  Array(10).fill(null).map(x => executor.addJob(f));

  await executor.execute();
})();

I have a few ideas about how to implement this on pure JS, but some implementations are already available? Also, I know that it's possible to implement with some 3-party soft, such as Bull queue and so on, but I need exactly in-process implementation.

Thanks in advance!

What are you seeking for is known as throttling . You can find it implemented in popular libraries like lodash's throttle or you can implement it in vanilla javascript like in this answer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM