Maximum concurrent requests for WebClient, HttpWeb

2019-02-16 21:08发布

Are there any limit to how many concurrent/parallel requests you can send using WebClient, HttpWebRequest, and HttpClient class in .NET 4.5? Or is it that you can send unlimited parallel requests if there is no restriction on the web API or server? Sorry if the question is too broad, please correct me so I can narrow it down.

1条回答
Root(大扎)
2楼-- · 2019-02-16 21:35

Yes, there is a limit. The default connection limit is 2 concurrent connections per remote host. This can be overridden. For example, I believe that ASP.NET by default overrides the default to be 10 connections per remote host.

From https://msdn.microsoft.com/en-us/library/7af54za5.aspx:

The number of connections between a client and server can have a dramatic impact on application throughput. By default, an application using the HttpWebRequest class uses a maximum of two persistent connections to a given server, but you can set the maximum number of connections on a per-application basis.

To change the connection limit yourself, use ServicePointManager.DefaultConnectionLimit. Make sure to make this change soon after your process starts up, before you start making HTTP connections to the remote host. This is because because once you actually make an HTTP connection to a remote host, then a ServicePoint will be created for that remote host using the current default connection limit.

Note that there are other effective limits on concurrent connections beyond what's enforced by the HTTP client classes. For example, if you wanted to open a million concurrent connections, you'd probably run out of RAM, or threads, or some other OS resource. If you're bumping up against those limits, then you'll need to ask another more general question about how to scale up a .net process to process a lot of concurrent I/O. But if you're only opening a few tens of connections then you should be fine.

BTW, if you're curious why the default limits are so low, it's actually baked into the HTTP specification-- the goal was to ensure that selfish HTTP clients didn't swamp HTTP servers with simultaneous connections. Although modern browsers ignore the spec. See http://www.stevesouders.com/blog/2008/03/20/roundup-on-parallel-connections/ for a lot more detail about this topic than you ever wanted to know!

查看更多
登录 后发表回答