💓 Performing TCP handshake without ACK in Go, useful for health checking, that is SYN, SYN-ACK, RST.
Go to file
Tevin 650050ef9b
Add special thanks to @jakubgs
2019-11-12 15:57:04 +08:00
app/tcp-checker Print error from CheckingLoop 2019-02-12 16:19:46 +08:00
.gitignore Initial commit 2016-05-31 18:19:07 +08:00
.travis.yml use x/sys/unix instead of syscall to fix EpollWait on Android 2019-11-11 11:47:34 +01:00
LICENSE Initial commit 2016-05-31 18:19:07 +08:00
README.md Add special thanks to @jakubgs 2019-11-12 15:57:04 +08:00
check_linux_test.go Use 10.255.255.1:80 as an always-timeout address 2019-02-13 11:05:36 +08:00
checker_linux.go use x/sys/unix instead of syscall to fix EpollWait on Android 2019-11-11 11:47:34 +01:00
checker_nonlinux.go Fix WaitReady() for nonlinux platform 2019-02-12 15:22:22 +08:00
checker_test.go Find an address that will actually timeout in tests by testing 2019-03-06 16:24:31 +08:00
doc.go Refine doc 2019-02-11 18:42:46 +08:00
err.go use x/sys/unix instead of syscall to fix EpollWait on Android 2019-11-11 11:47:34 +01:00
event.go Refactor 2019-02-11 17:54:51 +08:00
go.mod Update go.mod 2019-11-12 11:29:27 +08:00
go.sum use x/sys/unix instead of syscall to fix EpollWait on Android 2019-11-11 11:47:34 +01:00
pipe_pool.go Add PipePool of different implementations 2019-02-12 18:40:29 +08:00
pipe_pool_dummy.go Add PipePool of different implementations 2019-02-12 18:40:29 +08:00
pipe_pool_linux_test.go Add benchmarks 2019-02-12 19:03:06 +08:00
pipe_pool_sync_pool.go Add PipePool of different implementations 2019-02-12 18:40:29 +08:00
result_pipes.go Add ResultPipes of different implementations 2019-02-12 18:40:29 +08:00
result_pipes_linux_test.go Adjust timeout in benchmark 2019-03-06 16:24:31 +08:00
result_pipes_mu.go Add ResultPipes of different implementations 2019-02-12 18:40:29 +08:00
result_pipes_sync_map.go Add ResultPipes of different implementations 2019-02-12 18:40:29 +08:00
socket.go use x/sys/unix instead of syscall to fix EpollWait on Android 2019-11-11 11:47:34 +01:00
socket_linux.go use x/sys/unix instead of syscall to fix EpollWait on Android 2019-11-11 11:47:34 +01:00

README.md

TCP Checker 💓

Go Report Card GoDoc Build Status

This package is used to perform TCP handshake without ACK, which useful for TCP health checking.

HAProxy does this exactly the same, which is:

  1. SYN
  2. SYN-ACK
  3. RST

Why do I have to do this

In most cases when you establish a TCP connection(e.g. via net.Dial), these are the first three packets between the client and server(TCP three-way handshake):

  1. Client -> Server: SYN
  2. Server -> Client: SYN-ACK
  3. 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 server could be considered alive right after it sends back SYN-ACK,

that renders the last ACK unnecessary or even harmful in some cases.

Benefits

By avoiding the last ACK

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

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

This means the application level server will not notice the health checking traffic at all, thus the act of health checking will not be considered as some misbehavior of client.

Requirements

  • Linux 2.4 or newer

There is a fake implementation for non-Linux platform which is equivalent to:

conn, err := net.DialTimeout("tcp", addr, timeout)
conn.Close()

Usage

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

	c := NewChecker()

	ctx, stopChecker := context.WithCancel(context.Background())
	defer stopChecker()
	go func() {
		if err := c.CheckingLoop(ctx); err != nil {
			fmt.Println("checking loop stopped due to fatal error: ", err)
		}
	}()

	<-c.WaitReady()

	timeout := time.Second * 1
	err := c.CheckAddr("google.com:80", timeout)
	switch err {
	case ErrTimeout:
		fmt.Println("Connect to Google timed out")
	case nil:
		fmt.Println("Connect to Google succeeded")
	default:
		fmt.Println("Error occurred while connecting: ", err)
	}

TODO

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

Special thanks to contributors

  • @lujjjh
  • @jakubgs Fixed compatibility on Android