posted 3 years ago
I wanted to design and implement a rate limiter, which allows the following usage:
From my understanding, when somebody calls do
, the rate limiter needs to do one of two things with the passed-in lambda:
Start executing it immediately
Queue it up for later execution
In my case, if i call do
three times in rapid succession, the first two async functions should be immediately started while the third should be queued up.
Because the rate limiter needs to monitor the state of any async functions that it starts, it needs to install a callback on the returned Promise. It's easiest to use Promise.finally to do that. The finally
callback will run whether the promise is completed or rejected, so it'll handle both success and failure cases.
When any in-flight async function finishes and my installed Promise.finally
handler is called, i will need to determine if there are any queued async functions. If so, i will need to start one.
So there are essentially two "events" that the rate limiter is concerned with:
-Somebody calls do
-One of the spawned tasks completes (either successfully or unsuccessfully)
can anyone try to implement it?