Use the TCP option SO_LINGER with caution

Source: Internet
Author: User
1. Use the TCP option SO_LINGER with caution.

In a recent performance test, the communication program encountered an error, which showed that the server processing speed was significantly reduced. Check that many handles on the server are in the TIME_WAIT status, justin himself admitted that the test case was written incorrectly. After the recv function returns 0, there is no close handle.

I am very depressed. I found the SO_LINGER option on google, and found that this option can prevent the port from entering the TIME_WAIT status. I did a bold test. It is found that the performance of the server is quite good during the performance test.

// Use SO_LINGER and do not enter the TIME_WAIT status after close

Struct linger;

Linger. l_onoff = 1;

Linger. l_linger = 0;

Setsockopt (serverSocket,

SOL_SOCKET, SO_LINGER,

(Const char *) & linger,

Sizeof (linger ));

When I published the release on Wednesday, I suddenly found some errors reported by many colleagues. According to the log feedback from the client, the client often did not receive several response commands from our servers in the final stage, so I immediately thought that this option may cause trouble. After commenting on this option, everyone's program is normal.

Looking back, I carefully read "UNIX network programming" Volume 1 to remember it. The role of TIME_WAIT is to ensure that the data is received by the peer after the port is closed, Richard. steven's original saying is "TIME_WAIT is our friend."

Because our client needs to communicate with multiple game servers and there is a timing relationship between them, we cannot use this method to let users discard part of the data. I am not familiar with TCP details.

The TIME_WAIT status cannot be discarded, but it is also possible to shorten it. We can move our hands and feet on the l_linger parameter. When l_onoff is not 0. When Rochelle Linge is not 0, the set of interfaces will be delayed for a period of time on TIME_WAIT. Rochelle linger seconds, instead of 2MSL timeout [note]. I plan to continue the experiment.

 

[Note] According to volume 1 of UNIX network programming, it should be noted that the Rochelle linger non-0 interpretation may be different on different platforms, and the BSD platform implementation is 0.01 s.

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.