Too many TIME_WAIT solutions and Solutions
Recently, I used http_load for stress testing and ran a series of "Cannot assign requested address" errors. I checked the error and found that it was caused by too many TIME_WAIT requests. Because there are too many connections in a short period of time, a large number of ports are occupied, and the connection is closed and in the TIME_WAIT status. The port cannot be reused, so no port is available, so it's "Cannot assign requested address. Kernel parameters can be optimized: sysctl-w net. ipv4.tcp _ timestamps = 1 Enable TCP timestamp support. If this parameter is set to 0, the following setting does not work for sysctl-w net. ipv4.tcp _ tw_recycle = 1 indicates that echo 5000>/proc/sys/net/ipv4/tcp_max_tw_buckets is enabled for fast recovery of TIME-WAIT sockets in TCP connections, however, we still need to discuss why TIME_WAIT exists and what the above modification means. TIME_WAIT is the one that closes the TCP connection. It enters a state after receiving the FIN packet from the other party and giving an ACK response. The figure below is from UNP.
Why must the status of TIME_WAIT exist? Can I directly access CLOSEED? No. TCP is a reliable protocol established on an unreliable network. The ACK packet sent by the active party may be delayed, which triggers the FIN packet retransmission, is the 2MSL time. Therefore, you must have this status to ensure the reliability of TCP. Otherwise, when the re-transmitted FIN package arrives, two problems may occur: 1. the old connection is no longer available. Only the RST package can be returned. The party that passively closes cannot close the TCP connection. the new connection has been established, and the FIN package may interfere with the new connection. Therefore, TIME_WAIT cannot be absent, but it cannot be too many. You should limit the number of TIME_WAIT instances. Tcp_tw_recycle: as the name suggests is to recycle TIME_WAIT connection, note that timestamp must be opened, some people on the Internet said no, in fact, because timestamp is generally opened by default, there is a trap here, refer to http://www.pagefault.info /? P = 416. Tcp_max_tw_buckets: Obviously, it is used to control the number of TIME_WAIT instances. The TCP/IP protocol also has many things to know.