microsoft / cpprestsdk

The C++ REST SDK is a Microsoft project for cloud-based client-server communication in native code using a modern asynchronous C++ API design. This project aims to help C++ developers connect to and interact with services.
Other
7.97k stars 1.65k forks source link

Implement max concurrent connections #333

Open Robmaister opened 7 years ago

Robmaister commented 7 years ago

In the v2.4.0 release notes, the bullet point for deprecating http_client_config::set_guarantee_order() mentioned that in the future it would be better to expose an option that limits the max number of allowed concurrent connections. This is something I would be interested in implementing and contributing.

For the WinHTTP implementation, this appears straightfoward. For ASIO, it appears as though modifying asio_connection_pool is the best option (some guidance on the higher level structure of this patch would be appreciated). For WinRT, I couldn't find any documentation on limiting the number of connections at all.

I'm mostly interested in this as I am developing a background service for a client whose server seems to have trouble handling a large number of connections per client when the network connection is poor. When this happens a number of requests fail with error 12152 ERROR_HTTP_INVALID_SERVER_RESPONSE. The best solution currently is to drop down the number of parallel tasks from 40 to 2 (according to the client, dropping to 20 and 8 reduced the number of errors but did not eliminate them entirely). Simulating a slower network via Fiddler doesn't seem to help reproduce from a good connection either.

Even if this is a server configuration issue, I'm sure this feature would be useful for people who had background services that wanted to be light on system resources and would be willing to spend some time implementing it and writing some tests for it as well.

Overlordff commented 4 years ago

I agree with @Robmaister. Now, if I want to limit number of concurrent connections to 5, I create vector of 5 http_client instances. And set them set_guarantee_order to true. It is the simplest way to do that in the current API, I guess.

I believe that if we want to remove set_guarantee_order in future release, we should make simple way to set max concurrent connections too.