1

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?

Ph3n0m
  • 67
  • 7
  • Could you include an example of how you intend to use the `SemaphoreSlim`, and explain what problem emerges by the specific usage? – Theodor Zoulias Dec 04 '22 at 18:13
  • @TheodorZoulias I'm already keeping track of the number of calls I'm making, so I was thinking that I'd have `if` statements around the `SemaphoreSlim` waiting and releasing to only throttle when under load. – Ph3n0m Dec 04 '22 at 18:17

1 Answers1

2

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);
Mark Cilia Vincenti
  • 1,410
  • 8
  • 25