Is HTTP connection pooling possible?

2019-01-23 08:51发布

问题:

For a C# webservice that contacts a limited set of other servers, I wish to create 1 HTTP connection pool per server that I want to be able to contact.

The basic concept of course:

  • Each pool should open a few connections (3 connections?) to its remote webserver, and keep those connections alive.
  • A max-time-to-life should be used to recycle (disconnect/reconnect) the connections to the remote webserver, preventing the remote web server to disconnect before we do.
  • The connections should not be created simultaneously but with a little pause between the 3 connections so the recycling also does not happen simultaneously.
  • If the remote webserver still does disconnect unexpectedly, it should be noticed and we should reconnect.
  • If reconnecting is not possible for some reason, a retry should be done after a little pause.

This way, when I want to send a HttpWebRequest, I have ready-to-use connections, sparing the time of setting up the connection at the moment that I want to use it.

At the moment I don't know if this is even a default feature of HttpWebRequest. So sorry if I'm asking for the obvious. Googling for this only led me to similar questions for Java.

Question 1: is there such a thing present in .NET/c#?

Question 2: if not, is there a resource on this present on the internet, that you know of?

Question 3: if not, how to approach building one myself?

回答1:

HttpWebRequest (which essentially means all Http APIs in .net) already makes use of connection pooling by default.

Take a look at ServicePoint and ServicePointManager classes if you need to manage any of the parameters of the connection pool.