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!