9

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?

slugster
  • 49,403
  • 14
  • 95
  • 145
nl-x
  • 11,762
  • 7
  • 33
  • 61
  • Related: [HTTP Connection Pooling](http://stackoverflow.com/questions/15077526/http-connection-pooling) – slugster Feb 26 '13 at 09:47
  • Sorry if I misunderstood your question, or the context of it, but isn't this simply done by using WebSockets/long polling in .NET? SignalR would work nicely. – Chris Dixon Feb 26 '13 at 09:49
  • best explanation I found so far: https://www.stevejgordon.co.uk/httpclient-connection-pooling-in-dotnet-core – Sunny Sharma Jan 16 '20 at 08:00

1 Answers1

4

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.

spender
  • 117,338
  • 33
  • 229
  • 351
  • I have been analyzing the tcp packets to find out what is going on. It seems HttpWebRequest does reuse a connection if it can support keep-alive, but it does not actively try to keep it alive. Manually (without c#) I was able to keep a connection to Apache alive by just sending a (Is there no HTTP NOOP command?!) simple newline "\n" every 10 seconds to avoid the Apache Timeout of being reached, but I don't think this is an official way of doing it. I could have also done a HEAD request, that should also keep the connection alive, but requires Keep-Alive and generates more traffic – nl-x Feb 26 '13 at 12:57
  • What I would like to see is c# keeping the connection alive actively (just like what I was able to do manually). And after it disconnects for any reason (Timeout reached, KeepAliveTimout reached, MaxKeepAliveRequests reached) , that c# will reconnect directly and not wait until I want to use the connection again. – nl-x Feb 26 '13 at 13:04
  • Ow and BTW. I was able to see that HttpWebRequest uses the same ServicePoint over and over when it can. And confirmed it with the httpWebRequest.ServicePoint.GetHashCode(). But even though the connection is disconnected, before you do a new request, c# just reconnects quietly and returns the same hash-code. So ServicePoint and hashcode stay the same, but it IS a new connection, which is unfortunately only being established WHEN you try to use is. – nl-x Feb 26 '13 at 13:09
  • "but it does not actively try to keep it alive." - It's typically the server end that terminates the connection, i.e. look to see if you can configure a longer timeout on the HTTP server. E.g. in IIS the default is 2 mins. Of course, the client end can terminate the connection also. – redcalx Aug 05 '15 at 13:07