Tcp short connection TIME_WAIT solution (2) -- SO_LINGER

Source: Internet
Author: User

SO_LINGER is a socket option. It is easy to set through the setsockopt API, but its implementation mechanism is complicated and hard to understand literally.
The most clear explanation is the description in Unix Network Programming volume 1 (Chapter 7.5). Here is a simple excerpt:
The value of SO_LINGER is represented in the following data structure:
Struct linger {
Int l_onoff;/* 0 = off, nozero = on */
Int l_linger;/* linger time */

};


The value and processing are as follows:
1. If l_onoff is set to 0, this option is disabled. The value of l_linger is ignored, which is equal to the default kernel. The close call will be immediately returned to the caller, if possible, any unsent data will be transmitted;
2. If Rochelle off is set to non-0 and Rochelle linger is set to 0, TCP will terminate the connection when the set interface is closed. TCP will discard any data retained in the buffer zone sent by the set interface and send an RST to the other party,
Instead of the usual four-group termination sequence, this avoids the TIME_WAIT status;
3. Set Rochelle to non-zero and Rochelle to non-zero. When the set of interfaces is disabled, the kernel will be delayed for a period of time (determined by Rochelle linger ).
If data remains in the interface buffer, the process will be sleep until (a) all data is sent and confirmed by the other party, followed by a normal termination sequence (the descriptive word access count is 0)
Or (B) the delay time is up. In this case, it is very important for the application to check the return value of close. If the time is up before the data is sent and confirmed, close will return the EWOULDBLOCK error, and any data in the sending buffer zone of the interface is lost.
The successful return of close only tells us that the data (and fin) sent has been confirmed by the other party's TCP. It does not tell us whether the other party's application process has read the data. If the set of interfaces is set to non-blocking, it will not wait for close to complete.

The first case is actually the same as not set. The second case can be used to avoid the time_wait status. However, during testing on Linux, the RST option is not found, instead, the process is normally closed in four steps,
The preliminary conclusion is that "the RST is sent only when data is discarded". If the data is not discarded, the process is closed normally.
Check the Linux source code. There is indeed such a comment and source code:
===== Linux-2.6.37 net/IPv4/tcp. c 1915 ====
/* As outlined in RFC 2525, section 2.17, we send a RST here because
* Data was lost. to witness the awful effects of the old behavior
* Always doing a fin, run an older 2.1.x kernel or 2.0.x, start a bulk
* Get in an FTP client, suspend the process, wait for the client
* Advertise a zero window, then kill-9 the FTP client, Wheee...
* Note: timeout is always zero in such a case.
*/
If (data_was_unread ){
/* Unread data was tossed, zap the connection .*/
NET_INC_STATS_USER (sock_net (sk), LINUX_MIB_TCPABORTONCLOSE );
Tcp_set_state (sk, TCP_CLOSE );
Tcp_send_active_reset (sk, sk-> sk_allocation );
}
In principle, this option is risky and may cause data loss. Be careful when using it. However, we did not find this kind of phenomenon during the libmemcached test,
It should be related to the communication protocol settings of libmemcached, or it may be because we are under insufficient pressure to avoid this situation.

The third case is actually a compromise between the first and the second, and it does not work when the socket is not blocked.
For a large number of TIME_WAIT connection problems caused by short connections, I personally think that the second method is the best choice. libmemcached adopts this method,
According to the actual test, after this option is enabled, the number of TIME_WAIT connections is 0 and is not affected by network networking (such as whether a virtual machine or not.

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.