4

When Async/Await to make http requests (using HttpClient for example), is there any throttling built in by default?

Questions like this imply that an unlimited number of connections will be opened. My app, which performs a batch of http requests in a loop, seems to be stay capped at around 50 TCP connections.

I was initially concerned that I would need to add SemaphoreSlim for throttling, but .NET seems to doing this for me. Can anyone shed some light on this?

Community
  • 1
  • 1
Loren Paulsen
  • 8,960
  • 1
  • 28
  • 38
  • There's a limit of 2 connections per server. Are you connecting to the same servers? – i3arnon May 01 '15 at 00:14
  • Yes, in this case I'm hitting the same endpoint repeatedly in a bulk-load scenario. I know web browsers traditionally have had this 2 connection limitation, but .NET enforces it as well? How does that work? – Loren Paulsen May 01 '15 at 04:38
  • 1
    [Yes](http://stackoverflow.com/q/10403944/885318). – i3arnon May 02 '15 at 08:36

1 Answers1

6

There are limitations on connections in the .Net framework. For example this ServicePointManager has DefaultConnectionLimit That allows you to limit concurrent operations.

However, I wouldn't recommend using it for throttling. If you need control over your requests you should use SemaphoreSlim, specifically with WaitAsync as you're in an async-await environment and these requests are IO bound (and long).

Using SemaphoreSlim.WaitAsync means that you don't block and you wait asynchronously which can't be the case for any other non-async throttling mechanism. Also, you don't want to start the operation before you are able to complete it.

i3arnon
  • 113,022
  • 33
  • 324
  • 344