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

How to use RateLimiter to call api? #16

Closed
inventivethinking11 opened this issue May 30, 2019 · 4 comments
Closed

How to use RateLimiter to call api? #16

inventivethinking11 opened this issue May 30, 2019 · 4 comments

Comments

@inventivethinking11
Copy link

The example does not fit well with the actual API calling. In the scenario where API limits 5 calls per second but itself responds back in 3-4 seconds the example code just performs one call per second since its waiting for previous call to finish. How can I remedy it.

Example code

`

        //Create Time constraint: max five times by second
        var timeconstraint = TimeLimiter.GetFromMaxCountByInterval(5, TimeSpan.FromSeconds(1));
        //Use it
        for (int i = 0; i < 1000; i++)
        {
            await timeconstraint.Perform(ConsoleIt);
        }

      
    private async Task ConsoleIt()
    {
        Console.WriteLine(string.Format("{0:MM/dd/yyy HH:mm:ss.fff}", DateTime.Now));
        await Task.Delay(3000); //simulate api call made and waiting for response. 
    }`
@David-Desmaisons
Copy link
Owner

You do not need to wait for the call completion:

for (int i = 0; i < 1000; i++)
        {
            await timeconstraint.Perform(ConsoleIt);
        }

 private void ConsoleIt()
    {
        Console.WriteLine(string.Format("{0:MM/dd/yyy HH:mm:ss.fff}", DateTime.Now));
        Thread.Sleep(3000); //simulate api call made and waiting for response. 
    }`

@inventivethinking11
Copy link
Author

inventivethinking11 commented May 30, 2019

Thanks for the response.

But this does not seem to solve the problem. If you happen to execute this code, the system only makes a single call and waits until that finishes and starts next call.

`
public async Task Start()
{

        //Create Time constraint: max five times by second
        var timeconstraint = TimeLimiter.GetFromMaxCountByInterval(5, TimeSpan.FromSeconds(1));
        //Use it
        for (int i = 0; i < 1000; i++)
        {
            await timeconstraint.Perform(ConsoleIt);
        }

        return true;
    }
    private Task ConsoleIt()
    {
        Console.WriteLine(string.Format("{0:MM/dd/yyy HH:mm:ss.fff}", DateTime.Now));
        Thread.Sleep(3000); //simulate api call made and waiting for response. 
        Console.WriteLine("Finished");
        return Task.FromResult(0);

    }

`

@David-Desmaisons
Copy link
Owner

Note that in my example, I was using a signature with a void return and not a Task, this is key here not to wait to the completion of the task.
So:

for (int i = 0; i < 1000; i++)
        {
            await timeconstraint.Perform(ConsoleIt);
        }

 private void ConsoleIt()
    {
        Task.Run(() => {
         Console.WriteLine(string.Format("{0:MM/dd/yyy HH:mm:ss.fff}", DateTime.Now));
        Thread.Sleep(3000); //simulate api call made and waiting for response. 
     });
}

@inventivethinking11
Copy link
Author

Oh you are right. I didn't notice, that solved the issue.

Thanks!

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

2 participants