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 requests and responses, during which, the connection will also hold a native port in client side. When a single http client closes 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 connections with native ports in addition unavailable in client side within 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) at 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 such as socket buffer under control.
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 requests. 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 connections under control to a large extent.
reference:
https://cwiki.apache.org/confluence/display/HttpComponents/FrequentlyAskedConnectionManagementQuestions
subs:
1 21-ahttpclient 与TIME_WAIT
2 21-b并发tcp连接数与文件描述符 22文件描述符耗尽(一)mac【本地】 22文件描述符耗尽(二)linux【本地】
3 21-chttp连接池该取多大