[英]Async/Await maximum number of concurrent http requests
When Async/Await to make http requests (using HttpClient
for example), is there any throttling built in by default? 当Async / Await发出http请求时(例如使用
HttpClient
),默认情况下是否内置了任何限制?
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.
我的应用程序在一个循环中执行一批http请求,似乎保持在大约50个TCP连接上限。
I was initially concerned that I would need to add SemaphoreSlim
for throttling, but .NET seems to doing this for me. 我最初担心我需要添加
SemaphoreSlim
进行限制,但.NET似乎是为我做这个。 Can anyone shed some light on this? 任何人都可以对此有所了解吗?
There are limitations on connections in the .Net framework. .Net框架中的连接存在限制。 For example this
ServicePointManager
has DefaultConnectionLimit
That allows you to limit concurrent operations. 例如,此
ServicePointManager
具有DefaultConnectionLimit
,允许您限制并发操作。
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). 如果您需要控制您的请求,您应该使用
SemaphoreSlim
,特别是WaitAsync
因为您处于async-await
环境中,并且这些请求是IO绑定的(并且很长)。
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. 使用
SemaphoreSlim.WaitAsync
意味着您不会阻塞并等待异步,这对于任何其他非async
限制机制都不是这种情况。 Also, you don't want to start the operation before you are able to complete it. 此外,您不希望在能够完成操作之前启动该操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.