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?