Javascript - 带有速率限制器的进程内函数执行器?
Javascript - in-process function executor with rate limiter?
假设我有一些函数 f()
应该经常执行。但是我想为执行设置一个速率限制,比如"Execute function no more than 10 times in 1 minute"。在代码视图中,我认为它可以具有以下接口:
(async () => {
const f = async () => {};
const executor = new Executor({ max: 1, duration: 10000 });
Array(10).fill(null).map(x => executor.addJob(f));
await executor.execute();
})();
关于如何在纯 JS 上实现这个,我有一些想法,但是一些实现已经可用了?另外,我知道可以用一些3方软件来实现,比如Bull队列等等,但我需要的是进程内实现。
提前致谢!
您要寻找的是节流。您可以在 lodash's throttle or you can implement it in vanilla javascript like in this answer
等热门库中找到它的实现
假设我有一些函数 f()
应该经常执行。但是我想为执行设置一个速率限制,比如"Execute function no more than 10 times in 1 minute"。在代码视图中,我认为它可以具有以下接口:
(async () => {
const f = async () => {};
const executor = new Executor({ max: 1, duration: 10000 });
Array(10).fill(null).map(x => executor.addJob(f));
await executor.execute();
})();
关于如何在纯 JS 上实现这个,我有一些想法,但是一些实现已经可用了?另外,我知道可以用一些3方软件来实现,比如Bull队列等等,但我需要的是进程内实现。
提前致谢!
您要寻找的是节流。您可以在 lodash's throttle or you can implement it in vanilla javascript like in this answer
等热门库中找到它的实现