Documentation ¶
Overview ¶
Package tcp is used to perform TCP handshake without ACK, which useful for TCP health checking. HAProxy does this exactly the same, which is:
- SYN
- SYN-ACK
- 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):
- 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 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
- Less packets better efficiency
- 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.
Index ¶
- Variables
- type Checker
- func (c *Checker) CheckAddr(addr string, timeout time.Duration) (err error)
- func (c *Checker) CheckAddrZeroLinger(addr string, timeout time.Duration, zeroLinger bool) error
- func (c *Checker) CheckingLoop(ctx context.Context) error
- func (c *Checker) IsReady() bool
- func (c *Checker) PollerFDAtomic() int
- func (c *Checker) PollerFd() int
- func (c *Checker) WaitReady() <-chan struct{}
- type ErrConnect
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrCheckerAlreadyStarted = errors.New("Checker was already started")
ErrCheckerAlreadyStarted indicates there is another instance of CheckingLoop running.
var ErrTimeout = &timeoutError{}
ErrTimeout indicates I/O timeout
Functions ¶
This section is empty.
Types ¶
type Checker ¶
type Checker struct {
// contains filtered or unexported fields
}
Checker contains an epoll instance for TCP handshake checking
Example ¶
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) }
Output:
func NewCheckerZeroLinger ¶
NewCheckerZeroLinger creates a Checker with zeroLinger set to given value.
func (*Checker) CheckAddr ¶
CheckAddr performs a TCP check with given TCP address and timeout A successful check will result in nil error ErrTimeout is returned if timeout zeroLinger is an optional parameter indicating if linger should be set to zero for this particular connection Note: timeout includes domain resolving
func (*Checker) CheckAddrZeroLinger ¶
CheckAddrZeroLinger is like CheckAddr with an extra parameter indicating whether to enable zero linger.
func (*Checker) CheckingLoop ¶
CheckingLoop must be called before anything else. NOTE: this function blocks until ctx got canceled.
func (*Checker) PollerFDAtomic ¶
type ErrConnect ¶
type ErrConnect struct {
// contains filtered or unexported fields
}
ErrConnect is an error occurs while connecting to the host To get the detail of underlying error, lookup ErrorCode() in 'man 2 connect'