Git Product home page Git Product logo

tcp-shaker's Introduction

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

tcp-shaker's People

Contributors

tevino avatar frankxyy avatar lujjjh avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.