23Why httpclient is recommended to go with a connection pool in server-to-server request?

Posted silyvin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了23Why httpclient is recommended to go with a connection pool in server-to-server request?相关的知识,希望对你有一定的参考价值。

Why httpclient is recommended to go with a connection pool in server-to-server request?

There are 2 reasons that I think appear of great importance:

 

1 Http short connections contribute to big amounts of TIME_WAIT tcp connections who will hold many native ports

As known as usual, an http client will establish a tcp connection with the server transferring request and response, during which, the connection will also hold a native port in client side. When http client close a connection after receiving the response, a tcp FIN pack is sent to the server, and then the procedure would go in this way:

 

 

 

As shown in the pic, the connection will always stay until 2MSL (usually 1 min for 1MSL by default in linux) with the native port held even though the proceeding is down. As a result, there would be huge amounts of TIME_WAIT tcp connection and native ports not available in client side in 2MSL under high-frequency, which can be found out with command ‘netstat -anp|grep [server port].

For instance, 10,000 connections are established in one minute, which means 10,000 native ports will be unavailable in the following 2 minutes for the most part. Unfortunately, that will make influence on not only this proceeding nut also all other proceedings on the computer with new http requests in this period leaded to failure out of native ports which linux has 65535(2^16-1) as most.

 

To sum up, it is necessary to use a connection pool with http long connections to keep the occupation of native ports and tcp resources under control.

Other method will be SO_LINGER 18读缓冲区(滑动窗口)耗尽与write阻塞、拆包、延迟(四)linger  and SO_REUSEADDR 17tcp close端口占用 & setReuseAddress

 httpclient 与TIME_WAIT

 

2 Each http connection will hold a file

 

By default, a proceeding can only hold 1024 files at the same time, found out by command ‘ulimit -n‘ in linux. This restriction will possibly be touched under high-frequency http request. What makes the matter worse is that meanwhile jobs may not read any file due to the restriction pushed to failure caused by explosive http short connections of great quantity.

 

This situation can be avoided by a connection pool with fixed connect number as well as fixed number of opening files, which would contribute to keep file occupation of http connection under control to a large extent.

 并发tcp连接数与文件描述符

 

reference:

https://cwiki.apache.org/confluence/display/HttpComponents/FrequentlyAskedConnectionManagementQuestions

以上是关于23Why httpclient is recommended to go with a connection pool in server-to-server request?的主要内容,如果未能解决你的问题,请参考以下文章

Why is Blast useful?

Why malloc+memset is slower than calloc?

Why Orleans‘ actor is virutal

Why is Github ?

Why is HttpGet required only for some actions?

What Is Blockchain And Why Is WeChat And UnionPay Useing It ?