TIME_WAIT, CLOSE_WAIT, timewaitclosewait

Source: Internet
Author: User

TIME_WAIT, CLOSE_WAIT, timewaitclosewait

Contents

  • TCP connection release process
  • TIME_WAIT
    • The consequence of a large number of TiME_WAIT
    • Solution
  • CLOSE_WAIT
    • Consequences of a large number of CLOSE_WAIT
    • Solution
 

 

TCP connection release process

 

 

In the figure, A is the active party and B is the passive party.

It can be seen that if it is in the TIME_WAIT status, it indicates that it is disabled. If it is in CLOSE_WAIT, It is disabled passively.

 

TIME_WAIT

As shown in the figure, there will be two MSL (maximum message survival time) waiting times in the TIME_WAIT State before being in the CLOSED state and resources are released.

 

 

In order to ensure that the last ACk packet segment sent by A can reach B, it is actually to make B really close so that both sides of the connection can be closed. After all, it is full-duplex communication. The connection between A and B cannot be closed, and the connection between B and A is opened. This is only half-closed.

The default value of MSL is 120 s, that is, 2 minutes, so 2MSL is 4 minutes. That is to say, the party that closes the service will wait for four minutes to release the relevant resources.

 

TIME_WAITConsequences

1) if the client has a large number of TIME_WAIT instances, BindException will be reported if you want to use the port bound to the Socket again within four minutes.

2) If the server has a large number of TIME_WAIT, the service performance will decrease, because a large number of Sturct sockets are not released at the underlying layer, occupying the memory, which will be very slow when the number of users is very large.

3) when the server restarts, the port bound to the ServerSocket may be in the TIME_WAIT status, and the BindException exception may also occur.

 

TIME_WAITSolution

From the perspective of Program DesignLecture:

It is better for the server to avoid TIME_WAIT status (or less). After all, this problem is inevitable. That is, to distribute TIME_WAIT to the client, that is, to allow the client to actively close the connection.

From the O & M perspective:

You need to adjust the Operating System Configuration:

Net. ipv4.tcp _ syncookies = 1 indicates enabling SYN Cookies. When a SYN wait queue overflows, cookies are enabled to prevent a small number of SYN attacks. The default value is 0, indicating that the service is disabled. net. ipv4.tcp _ tw_reuse = 1 indicates that reuse is enabled. Allow TIME-WAIT sockets to be re-used for a New TCP connection. The default value is 0, indicating that it is disabled. net. ipv4.tcp _ tw_recycle = 1 indicates that fast recovery of TIME-WAIT sockets in TCP connections is enabled. The default value is 0, indicating that the quick recovery is disabled. Net. ipv4.tcp _ fin_timeout: modifies the default TIMEOUT time of the system, in seconds.

CLOSE_WAIT

As shown in the preceding TCP connection release process, if either party has a CLOSE_WAIT status, the FIN packet segment is not sent to the other party after the connection is passively closed.

    

 

By default, the Keepalive time set by the operating system is 2 hours.

 

A large number of CLOSE_WAITConsequences

If the connection occurs on the client, the connection is closed by the server. These ports on the client cannot be used in this CLOSE_WAIT status. If you want to use it, BindExcepion should be reported.

If it occurs on the server, it indicates that the client is closed, and the server will also become slower. In this case, you need to find the cause on the server.

 Solution

From the perspective of Program Design:

1) when the connection is closed at the other end, the reason why the CLOSE_WAIT end does not execute the close connection operation is displayed.

2) if this occurs on the client, check why the server needs to close these connections. The server should not close the connection.

If you solve this problem from the code, you should execute the close () method of Socket (or SocketChannel. When this method is executed, the FIN packet segment is sent.

From the O & M perspective:

Tcp_keepalive_time: the default INTEGER value is 7200 (2 hours). When keepalive is enabled, TCP sends the keepalive message frequency. (Due to factors such as network attacks, this attack is very frequent. Some cu friends once mentioned that if a connection is established on both sides, if no data or rst/fin messages are sent, will the duration be 2 hours or empty connection attacks? Tcp_keepalive_time is used to prevent this situation. I personally changed the value to 1800 seconds when doing the nat service) tcp_keepalive_probes: the default value of INTEGER is the number of times 9TCP sends the keepalive test to determine whether the connection has been disconnected. (Note: the connection is sent only when the SO_KEEPALIVE socket option is enabled. you do not need to modify the number of times by default. You can also shorten the value as needed. set to 5.) tcp_keepalive_intvl: the default value of INTEGER is 75. Multiply the value by tcp_keepalive_probes to get the time for killing connections that have not responded since the start of the test. The default value is 75 seconds, indicating that connections without activity will be dropped after about 11 minutes. (For common applications, this value is too large and can be changed as needed. Especially for web servers, this value needs to be changed to a smaller value. 15 is a suitable value)

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.