Original: Coding diary (wechat official ID: Codelogs), welcome to share, reprint please reserve the source.

scenario

Written code after period of time, the connection pool should not strange, MySQL, HttpClient and MQ, these all need connection pool, and in general, the server program will automatically disconnect for a long time not interaction, but the TCP connection is closed, unable to perceive the end so connection ChiZu pieces are generally need to configure a keep alive time, The connection pool component needs this value to determine whether the connection is alive, so how do you configure this value?

Check the idle disconnection time of the server

Packet capture software, such as wireshark, can send SYN packets based on TCP connections and FIN packets based on disconnection. You can subtract the SYN packet time from the FIN packet time, which is the allowed idle time on the server. But we can use Telnet command to detect, as follows:

$ time telnet www.zhihu.com 443
Trying 58.49.159.14...
Connected to 1595096.sched.d0-dk.tdnsv5.com.
Escape character is '^]'.
Connection closed by foreign host.
 The real 0 m15. 128 sThe user 0 m0. The 000 sSys 0 m0. The 000 sCopy the code

Alternatively, use the ncat command as follows:

$ time ncat --recv-only -v www.zhihu.com 443
Ncat: Version 7.60 (https://nmap.org/ncat)Ncat: Connected to 58.49.157.164:443.
Ncat: 0 bytes sent, 0 bytes received in 15.12 seconds.

The real 0 m15. 132 sThe user 0 m0. The 000 sSys 0 m0. The 031 sCopy the code

The real displayed in the time command is the maximum idle time. It can be seen that the idle time of the TCP connection is 15s. Set it to the connection pool and set it to a smaller value, such as 12s

conclusion

After configuring the connection pool connection idle time, do not beat the head, use this method!

Content of the past

Curl curl curl curl curl curl curl curl curl