|
| 1 | +// Package tcp is used to perform TCP handshake without ACK. |
| 2 | +// Useful for health checking, HAProxy do this exactly the same. |
| 3 | +// Which is SYN, SYN-ACK, RST. |
| 4 | +// |
| 5 | +// Why do I have to do this? |
| 6 | +// Usually when you establish a TCP connection(e.g. net.Dial), these |
| 7 | +// are the first three packets (TCP three-way handshake): |
| 8 | +// |
| 9 | +// SYN: Client -> Server |
| 10 | +// SYN-ACK: Server -> Client |
| 11 | +// ACK: Client -> Server |
| 12 | +// |
| 13 | +// This package tries to avoid the last ACK when doing handshakes. |
| 14 | +// |
| 15 | +// By sending the last ACK, the connection is considered established. |
| 16 | +// However as for TCP health checking the last ACK may not necessary. |
| 17 | +// The Server could be considered alive after it sends back SYN-ACK. |
| 18 | +// |
| 19 | +// Benefits of avoiding the last ACK: |
| 20 | +// |
| 21 | +// 1. Less packets better efficiency |
| 22 | +// |
| 23 | +// 2. The health checking is less obvious |
| 24 | +// |
| 25 | +// The second one is essential, because it bothers server less. |
| 26 | +// Usually this means the server will not notice the health checking |
| 27 | +// traffic at all, thus the act of health chekcing will not be |
| 28 | +// considered as some misbehaviour of client. |
| 29 | +package tcp |
| 30 | + |
| 31 | +import ( |
| 32 | + "fmt" |
| 33 | + "os" |
| 34 | + "runtime" |
| 35 | + "syscall" |
| 36 | + "time" |
| 37 | +) |
| 38 | + |
| 39 | +const maxEpollEvents = 32 |
| 40 | + |
| 41 | +// Shaker contains an epoll instance for TCP handshake checking |
| 42 | +type Shaker struct { |
| 43 | + epollFd int |
| 44 | +} |
| 45 | + |
| 46 | +// Init creates inner epoll instance, call this before anything else |
| 47 | +func (s *Shaker) Init() error { |
| 48 | + var err error |
| 49 | + s.epollFd, err = syscall.EpollCreate1(0) |
| 50 | + if err != nil { |
| 51 | + return os.NewSyscallError("epoll_create1", err) |
| 52 | + } |
| 53 | + return nil |
| 54 | +} |
| 55 | + |
| 56 | +// Test performs a TCP check with given TCP address and timeout |
| 57 | +// A successful check will result in nil error |
| 58 | +// ErrTimeout is returned if timeout |
| 59 | +// Note: timeout includes domain resolving |
| 60 | +func (s *Shaker) Test(addr string, timeout time.Duration) error { |
| 61 | + deadline := time.Now().Add(timeout) |
| 62 | + |
| 63 | + rAddr, err := parseSockAddr(addr) |
| 64 | + if err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + |
| 68 | + fd, err := createSocket() |
| 69 | + if err != nil { |
| 70 | + return err |
| 71 | + } |
| 72 | + defer syscall.Close(fd) |
| 73 | + |
| 74 | + if err = setSockopts(fd); err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + |
| 78 | + if err = s.connect(fd, rAddr, deadline); err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + if reached(deadline) { |
| 82 | + return ErrTimeout |
| 83 | + } |
| 84 | + |
| 85 | + s.addEpoll(fd) |
| 86 | + timeoutMS := int(timeout.Nanoseconds() / 1000000) |
| 87 | + // check for connect error |
| 88 | + for { |
| 89 | + succeed, err := s.wait(fd, timeoutMS) |
| 90 | + if err != nil { |
| 91 | + return fmt.Errorf("connect error: %s", err) |
| 92 | + } |
| 93 | + if reached(deadline) { |
| 94 | + return ErrTimeout |
| 95 | + } |
| 96 | + if succeed { |
| 97 | + return nil |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +// Close closes the inner epoll fd |
| 103 | +func (s *Shaker) Close() error { |
| 104 | + return syscall.Close(s.epollFd) |
| 105 | +} |
| 106 | + |
| 107 | +func (s *Shaker) addEpoll(fd int) error { |
| 108 | + var event syscall.EpollEvent |
| 109 | + event.Events = syscall.EPOLLOUT |
| 110 | + event.Fd = int32(fd) |
| 111 | + if err := syscall.EpollCtl(s.epollFd, syscall.EPOLL_CTL_ADD, fd, &event); err != nil { |
| 112 | + return os.NewSyscallError("epoll_ctl", err) |
| 113 | + } |
| 114 | + return nil |
| 115 | +} |
| 116 | + |
| 117 | +// wait waits for epoll event of given fd |
| 118 | +// The boolean returned indicates whether the connect is successful |
| 119 | +func (s *Shaker) wait(fd int, timeoutMS int) (bool, error) { |
| 120 | + var events [maxEpollEvents]syscall.EpollEvent |
| 121 | + nevents, err := syscall.EpollWait(s.epollFd, events[:], timeoutMS) |
| 122 | + if err != nil { |
| 123 | + return false, os.NewSyscallError("epoll_wait", err) |
| 124 | + } |
| 125 | + |
| 126 | + for ev := 0; ev < nevents; ev++ { |
| 127 | + if int(events[ev].Fd) == fd { |
| 128 | + errCode, err := syscall.GetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_ERROR) |
| 129 | + if err != nil { |
| 130 | + return false, os.NewSyscallError("getsockopt", err) |
| 131 | + } |
| 132 | + if errCode != 0 { |
| 133 | + return false, fmt.Errorf("getsockopt[%d]", errCode) |
| 134 | + } |
| 135 | + return true, nil |
| 136 | + } |
| 137 | + } |
| 138 | + return false, nil |
| 139 | +} |
| 140 | + |
| 141 | +func (s *Shaker) connect(fd int, addr syscall.Sockaddr, deadline time.Time) error { |
| 142 | + switch err := syscall.Connect(fd, addr); err { |
| 143 | + case syscall.EINPROGRESS, syscall.EALREADY, syscall.EINTR: |
| 144 | + case nil, syscall.EISCONN: |
| 145 | + // already connected |
| 146 | + case syscall.EINVAL: |
| 147 | + // On Solaris we can see EINVAL if the socket has |
| 148 | + // already been accepted and closed by the server. |
| 149 | + // Treat this as a successful connection--writes to |
| 150 | + // the socket will see EOF. For details and a test |
| 151 | + // case in C see https://golang.org/issue/6828. |
| 152 | + if runtime.GOOS == "solaris" { |
| 153 | + return nil |
| 154 | + } |
| 155 | + fallthrough |
| 156 | + default: |
| 157 | + return os.NewSyscallError("connect", err) |
| 158 | + } |
| 159 | + return nil |
| 160 | +} |
| 161 | + |
| 162 | +func reached(deadline time.Time) bool { |
| 163 | + return !deadline.IsZero() && deadline.Before(time.Now()) |
| 164 | +} |
0 commit comments