Some time ago, colleagues found that a thrift theadpoolserver as a TCP service appeared a large number of close_wait state of the socket. When you first encounter this problem, you may have the following questions: What is close_wait? Why would it happen? There are other similar states I don't know.
In fact, what you really want to ask is what happens when the TCP channel shuts down. What am I going to do.
A TCP channel is a connection in which both sides of the connection can write data to the channel or read data from the channel, and both ends of the connection can initiate a shutdown operation. The shutdown process for the entire TCP channel is as follows:
A (socketfd:10) <--–tcp connction ———->b (socketfd:20) closes a, then a sends a fin to B; b After receiving the FIN, return an ACK indicating that the shutdown notification was received, and when B returned the ACK, the status was changed to close_wait(. ), what is it waiting for? Wait for the program to explicitly (manually) close socket B. If the program that opens B does not do any processing after issuing an ACK, it is always in the close_wait. If the program explicitly shuts down B, then B sends a fin to a, then B is in the Last_ack state, and a The last ACK is issued after receiving the fin of B, at which point A is in the well-known time_wait state. Time_wait time is usually longer.
How to avoid close_wait? Too much close_wait usually means there is a serious problem in your program: do not properly handle the shutdown of the socket, to see the code.
how to avoid time_wait? Usually, this is a question that should not be asked, should ask oneself first, time_wait really bring you trouble? Time_wait is the guarantee mechanism of reliability, there is no need to avoid it. But maybe, you can try using RFC1323. If there is too much time_wait at one end of the problem, I suggest the strategy is: try to avoid time_wait too much of the end of the active closed socket socketpool, avoid frequent create/close socket