b1b6beb4d3 | ||
---|---|---|
.gitignore | ||
LICENSE | ||
README.md | ||
err.go | ||
shaker.go | ||
shaker_test.go | ||
socket.go |
README.md
TCP Shaker 💓
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:
- Less packets better efficiency
- 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)