💓 Performing TCP handshake without ACK in Go, useful for health checking, that is SYN, SYN-ACK, RST.
Go to file
Tevin Zhang b1b6beb4d3 Add usage in README 2016-06-01 18:23:31 +08:00
.gitignore Initial commit 2016-05-31 18:19:07 +08:00
LICENSE Initial commit 2016-05-31 18:19:07 +08:00
README.md Add usage in README 2016-06-01 18:23:31 +08:00
err.go Better error handling 2016-06-01 17:47:04 +08:00
shaker.go Fix typo 2016-06-01 18:18:41 +08:00
shaker_test.go Better error handling 2016-06-01 17:47:04 +08:00
socket.go First commit 2016-06-01 13:42:41 +08:00

README.md

TCP Shaker 💓

GoDoc

Performing TCP handshake without ACK, useful for health checking.

HAProxy do this exactly the same, which is:

  • SYN
  • SYN-ACK
  • RST

Why do I have to do this?

Usually when you establish a TCP connection(e.g. net.Dial), these are the first three packets (TCP three-way handshake):

  • Client -> Server: SYN
  • Server -> Client: SYN-ACK
  • Client -> Server: ACK

This package tries to avoid the last ACK when doing handshakes.

By sending the last ACK, the connection is considered established.

However as for TCP health checking the last ACK may not necessary.

The Server could be considered alive after it sends back SYN-ACK.

Benefits of avoiding the last ACK:

  1. Less packets better efficiency
  2. The health checking is less obvious

The second one is essential, because it bothers server less.

Usually this means the server will not notice the health checking traffic at all, thus the act of health checking will not be considered as some misbehaviour of client.

Requirements:

  • Linux 2.4 or newer

Usage

	import "github.com/tevino/tcp-shaker"

	s := tcp.Shaker{}
	if err := s.Init(); err != nil {
		log.Fatal("Shaker init failed:", err)
	}

	timeout := time.Second * 1
	err := s.Test("google.com:80", timeout)
	switch err {
	case tcp.ErrTimeout:
		fmt.Println("Connect to Google timeout")
	case nil:
		fmt.Println("Connect to Google succeded")
	default:
		if e, ok := err.(*tcp.ErrConnect); ok {
			fmt.Println("Connect to Google failed:", e)
		} else {
			fmt.Println("Error occurred while connecting:", err)
		}
	}

TODO:

  • IPv6 support (Test environment needed, PRs are welcomed)