简体   繁体   中英

Throttling threads in C#

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 (ie you need to release 2 before a new one can enter), but with SemaphoreSlimThrottle you can.

var mySemaphore = new SemaphoreSlimThrottle(-1, 10);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM