Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tevino/tcp-shaker
:heartbeat: Perform TCP handshake without ACK in Go, useful for health check, that is SYN, SYN-ACK, RST.
https://github.com/tevino/tcp-shaker
ack go golang golang-library golang-package handshake haproxy health-check linux network packets tcp
Last synced: 4 days ago
JSON representation
:heartbeat: Perform TCP handshake without ACK in Go, useful for health check, that is SYN, SYN-ACK, RST.
- Host: GitHub
- URL: https://github.com/tevino/tcp-shaker
- Owner: tevino
- License: mit
- Created: 2016-05-31T10:19:07.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2024-01-03T09:47:55.000Z (10 months ago)
- Last Synced: 2024-08-02T15:47:57.040Z (3 months ago)
- Topics: ack, go, golang, golang-library, golang-package, handshake, haproxy, health-check, linux, network, packets, tcp
- Language: Go
- Homepage:
- Size: 69.3 KB
- Stars: 411
- Watchers: 9
- Forks: 55
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
- my-awesome - tevino/tcp-shaker - library,golang-package,handshake,haproxy,health-check,linux,network,packets,tcp pushed_at:2024-01 star:0.4k fork:0.1k :heartbeat: Perform TCP handshake without ACK in Go, useful for health check, that is SYN, SYN-ACK, RST. (Go)
README
# TCP Checker :heartbeat:
[![Go Report Card](https://goreportcard.com/badge/github.com/tevino/tcp-shaker)](https://goreportcard.com/report/github.com/tevino/tcp-shaker)
[![GoDoc](https://godoc.org/github.com/tevino/tcp-shaker?status.svg)](https://godoc.org/github.com/tevino/tcp-shaker)
[![Build Status](https://travis-ci.org/tevino/tcp-shaker.svg?branch=master)](https://travis-ci.org/tevino/tcp-shaker)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. RSTThis implementation has been running on tens of thousands of production servers for years.
## 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][tcp-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 obviousThe 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:
```go
conn, err := net.DialTimeout("tcp", addr, timeout)
conn.Close()
```The reason for a fake implementation is that there is currently no way to perform an equivalent operation on certain platforms, specifically macOS. A fake implementation is a degradation on those platforms and ensures a successful compilation.
## Usage
```go
import "github.com/tevino/tcp-shaker"// Initializing the checker
// It is expected to be shared among goroutines, only one instance is necessary.
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()
// Checking google.com
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
- [x] IPv6 support (Test environment needed, PRs are welcome)
## Special thanks to contributors
- @lujjjh Added zero linger support for non-Linux platform
- @jakubgs Fixed compatibility on Android
- @kirk91 Added support for IPv6[tcp-handshake]: https://en.wikipedia.org/wiki/Handshaking#TCP_three-way_handshake