Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a throttled API without hitting limit?

I'm trying to figure out how to use an API that has a limit of 5 requests per second efficiently without hitting the limit. If a request hits the limit I will get an error response from the API. Thread.Sleep(200) does not feel like a proper solution as I need efficiency.

foreach (Foo foo in fooList)
{
    try
    {
        // Max 5 API calls per second! How?
        ThirdPartyResponse response = ContactThirdPartyApi(foo);

        // do something with response
        ...
    }
}

Preferably I would like to use some kind of background workers for the calls but how do I control the nr of request per second made? I've tried looking at for example Hangfire to solve this but it doesn't really seem to work for this type of limit control.

As I can fire request and know when I get the response it should be possible to keep track of how many has been fired & completed and to make sure no more than 5 per second happens. But how? Any and all suggestions are welcome!

like image 754
Niklas Jonsson Avatar asked Sep 30 '22 19:09

Niklas Jonsson


1 Answers

You can limit the number of concurrent requests using a Semaphore or SemaphoreSlim, then you would need to add a time element to when it can be released.

Joel Fillmore has written a TimeSpanSemaphoreSlim implementation that you could incorporate into your API calling implementation. IIRC, I combined it successfully with RestSharp for a previous project.

You may want to modify the implementation slightly to set the resolution of Thread.Sleep(1) properly.

like image 89
Russ Cam Avatar answered Oct 08 '22 12:10

Russ Cam