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

Best way to compose a limit on a large timespan. #34

Open
mattheys opened this issue Dec 10, 2021 · 0 comments
Open

Best way to compose a limit on a large timespan. #34

mattheys opened this issue Dec 10, 2021 · 0 comments

Comments

@mattheys
Copy link

Hi,

I'm trying to consume an API that iterates a folder and file structure and that has a limit of 2000 calls per 600 seconds.

I'm trying to come up with the best approach so that after the 600 seconds I am as close to using all 2000 calls possible without exhausting all the calls in the first 100 seconds and letting the user wait 500 seconds for the next update.

var constraint = new CountByIntervalAwaitableConstraint(4, TimeSpan.FromSeconds(1));
var constraint2 = new CountByIntervalAwaitableConstraint(1950, TimeSpan.FromSeconds(600));
limiter = TimeLimiter.Compose(constraint, constraint2);

I've gone with this to start with, but after the 600 seconds were up I still had anywhere between 50 to 600 requests remaining meaning I was leaving performance on the table.

To note is that the API also returns how many requests I have left and how long until the next reset period if I can use those values somehow as well.

When my service is left to iterate a folder structure on it's own it's more like 50 requets remaining at the end, however if the time period included a time when the user was left to select some options before clicking start then it can be more like 600.

Basically I want it to start off with about 3 requests per second, then if for some reason it isn't going to get close to 0 calls left at the end have the ability to speed up and use all the available performance.

Can I reassign to the private field? Is it thread safe to do so? Could after every 100 calls used re-evaluate the remaining performance and regenerate the limiter?
e.g.

var timeLeft = 200; //seconds
var callsLeft = 1200; //api calls

var callsPerSecond = callsLeft/timeLeft;
var constraint1 = new CountByIntervalAwaitableConstraint(callsPerSecond, TimeSpan.FromSeconds(1));
var constraint2 = new CountByIntervalAwaitableConstraint(callsLeft , TimeSpan.FromSeconds(timeLeft));
limiter = TimeLimiter.Compose(constraint1, constraint2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant