Go language uses TCP keepalive

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

English Original: Using TCP keepalive with Go

If you write some TCP socket code, you may wonder: What happens if the network cable is dialed off or if the remote host crashes my TCP connection?

The short answer is: none of the effects. In this scenario, the end of the connection to the remote host is not sent by the FIN packet, and the local system cannot detect whether the connection has been interrupted. So you need to solve this situation as a programmer.

--zxp
Translated 1 months ago

0 Person Top

top translation of good Oh!

Go language gives you several ways to solve this problem. The preferred method might be net. The Setreaddeadline method in the Conn interface. Assuming that your connection is receiving data at a specific interval, you can simply treat the read timeout as an IO. EOF error and close this connection. Many existing TCP protocols support this method of handling errors by defining a heartbeat mechanism or service Health 1, which sends Ping/pong probe packets at specific intervals between endpoints to detect both network problems. In addition, this heartbeat mechanism may also help the Proxy Server view network activity to determine the health quality of the connection.

--ZXP
translated 1 months ago

0 Human Top

top   Good translation!

So if your protocol supports heartbeat, or you can If the protocol joins the heartbeat, the solution should be the first choice to solve the problem of network drop.

However, if you have no control over the protocol and it does not support the heartbeat, what should you do?

Now is the time to learn about tcp keepalive and use it in go. Tcp keepalive is defined in RFC 1122, but is not part of the TCP specification. It can be enabled in an individual connection, but it must be turned off by default. Enabling it causes the network stack to probe the connectivity status of the connection after a certain period of inactivity (not less than 2 hours). The probe packet cannot contain data 2, and the failure of a probe packet's reply cannot treat the connection as interrupted because the transmission of the probe packet is unreliable.

--ZXP
translated 1 months ago

0 Human Top

top   Good translation!

GO can be via net. Tcpconn setkeepalive to enable TCP KeepAlive On OS X and Linux systems, when a connection space is 2 hours, 8 TCP keepalive probe packets are sent at 75-second intervals. In other words, after 2 hours and 10 minutes (7200+8*75), read will return an IO. EOF error.

For your app, this time-out interval may be too long. In this case you can call the Setkeepaliveperiod method. However, this method will behave differently on different operating systems. On OSX It changes the space time of the connection before the probe packet is sent. On Linux It changes the space time of the connection and the sending interval of the probe packet. So calling  setkeepaliveperiod on a 30-second parameter will result in a total of 10 minutes and 30 seconds (30+8*75) of time-out on the OSX system, but on Linux it is 4 minutes and 30 seconds (30+8*30).

--ZXP
translated 1 months ago

0 Human top

top   translation is good Oh!

I found this situation very unsatisfactory, so I created a The package, called Tcpkeepalive, is used to provide you with more control:

kaconn, _  := tcpkeepalive. Enablekeepalive (conn) Kaconn.setkeepaliveidle (30*time. Second) Kaconn.setkeepalivecount (4) kaconn.setkeepaliveinterval (5*time. Second) 

Currently, only Linux and OS X are supported, but I am happy to merge pull requests on other platforms. If members of the Go core team are interested, I would also like to try to contribute these new methods to the go itself.

Please let me know if you think this article is valuable, if you have any questions, please point it out, and please indicate any errors so that I can make corrections.


0x0bject
translated 1 months ago

0 Human top

top   Good translation!

Appendix

1) It is tricky to tune a heartbeat failure early through a low, and untrue, detection rate. You can check out ϕaccrual Failure Detector to get a statistical model, as well as Gryski extension with Damian go-failure. Unfortunately, I can not think of any way to use it in the keepalive mechanism.

2) According to RFC 1122 KeepAlive subsection, there may be a single garbage octal number in the compatibility of fragmented implementations. However, I am not sure if the system network stack is filtered out, if you know, please comment comments below.

0x0bject
Translated 1 months ago

0 Person Top

top translation of good Oh!

All translations in this article are for learning and communication purposes only, please be sure to indicate the translator, source, and link to this article.
Our translation work in accordance with the CC agreement, if our work has violated your rights and interests, please contact us promptly

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.