简体   繁体   English

异步/等待最大并发http请求数

[英]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. 我的应用程序在一个循环中执行一批h​​ttp请求,似乎保持在大约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.

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