energizer91 / smart-request-balancer

Smart request queue with fine tuning of rate and limit of requests
MIT License
97 stars 8 forks source link

un-optimized rate throttling #6

Closed regevbr closed 5 years ago

regevbr commented 5 years ago

I believe that there is a major issue with the optimization level regarding the start time of scheduled queue items.

Lets follow the following scenario:

  1. Set up a queue that allows 2 requests per second.
  2. Add 2 requests (x1, x2) to the queue that take 10 seconds each to complete.

Expected behavior:

  1. at t=0 x1 starts.
  2. at t=500 x2 starts.

Current behavior:

  1. at t=0 x1 starts.
  2. at t=10000 x2 starts.

The reason for current behavior is the fact that you call execute() only after x1 finishes, and not after it starts.

@energizer91 what do you think? Am I missing something here ?

energizer91 commented 5 years ago

Good day! Yes, that is true, the only option how x2 can start is only after x1 is finished. That's because balancer starts to find next execution candidate in 2 cases:

If queue is not empty, executing promise will just add it to queue. That's a good point to improve library, thank you!

regevbr commented 5 years ago

Sure thing! Working on PR now