Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
299 views
in Technique[技术] by (71.8m points)

c# - Is HTTP connection pooling possible?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...