Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: allow throttled workers to have different cost #45

Open
RKushnir opened this issue Mar 11, 2018 · 3 comments
Open

Feature request: allow throttled workers to have different cost #45

RKushnir opened this issue Mar 11, 2018 · 3 comments
Labels
enhancement help wanted Contributions are highly appreciated

Comments

@RKushnir
Copy link

Let's say an API has a limit of 100 calls per minute. And there's 2 kinds of jobs — one makes 2 calls, the other makes 3.

To calculate the guaranteed limit I need to use the higher value, so the threshold will be 33 jobs/min. But this is obviously inefficient when most of the jobs are of the first kind(2 calls), in the worst case reducing the effective limit to 66 calls/min instead of 100.

So, it would be nice to be able to specify how much each job costs. Ideally, with an option to dynamically calculate the value.

@ixti
Copy link
Owner

ixti commented Mar 12, 2018

Oh. That sounds pretty interesting! I'll need to think about it. In fact I'm already slowly working on changing the throttling code (making it separated of sidekiq sot that sidekiq-throttled will use it eventually) - but that's a slowly going process as here in SensorTower we don't need this ATM.

But the idea you described seems pretty interesting to me. At least at this very moment. :D

@ixti
Copy link
Owner

ixti commented Mar 12, 2018

Can you describe in pseudo code how do you see it?

@RKushnir
Copy link
Author

I haven't ever written a sidekiq middleware, so I have no idea what I'm talking about :)

Anyway, as I understand the current implementation, it keeps a list of the timestamps when previous jobs where started. And when there's a new job, it counts the timestamps to check if the limit has not been exceeded, in pseudo-SQL:

if (select count(*) from jobs where jobs.timestamp > 1.minute.ago) < 100 then execute

With my suggestion, it would store a pair (timestamp, job cost), and the calculation would be:

if (select sum(jobs.cost) from jobs where jobs.timestamp > 1.minute.ago) + new_job.cost <= 100 then execute

@ixti ixti added enhancement help wanted Contributions are highly appreciated labels May 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement help wanted Contributions are highly appreciated
Projects
None yet
Development

No branches or pull requests

2 participants