I have a situation where I am doing WebAPI calls to a service which sometimes cannot keep up with the load. I'm keeping track of how long the WebAPI takes to respond and how many active threads I have, and when the WebAPI is taking long to respond I want to limit the number of concurrent calls to a specific number (which I'd save in a config).
What I was thinking was of using SemaphoreSlim
but the problem would be that at that time the number of concurrent threads would most likely be greater than the number I'm limiting to. Is there a neat way of tackling this problem that wouldn't involve reinventing the wheel?
I have created a library that does just that. It's called SemaphoreSlimThrottling
and is available on GitHub and NuGet. Suppose you had 11 concurrent threads and wanted to limit them to 10. SemaphoreSlim
doesn't allow you to initialize with an initialCount
of -1 (i.e. you need to release 2 before a new one can enter), but with SemaphoreSlimThrottle
you can.
var mySemaphore = new SemaphoreSlimThrottle(-1, 10);