NetDialerWithTCPKeepalive returns a net.Dialer that enables TCP keepalives on the underlying connection with OS default values for keepalive parameters. TODO: Once https://github.com/golang/go/issues/62254 lands, and the appropriate Go version becomes less than our least supported Go version, we sh
()
| 34 | // appropriate Go version becomes less than our least supported Go version, we |
| 35 | // should look into using the new API to make things more straightforward. |
| 36 | func NetDialerWithTCPKeepalive() *net.Dialer { |
| 37 | return &net.Dialer{ |
| 38 | // Setting a negative value here prevents the Go stdlib from overriding |
| 39 | // the values of TCP keepalive time and interval. It also prevents the |
| 40 | // Go stdlib from enabling TCP keepalives by default. |
| 41 | KeepAlive: time.Duration(-1), |
| 42 | // This method is called after the underlying network socket is created, |
| 43 | // but before dialing the socket (or calling its connect() method). The |
| 44 | // combination of unconditionally enabling TCP keepalives here, and |
| 45 | // disabling the overriding of TCP keepalive parameters by setting the |
| 46 | // KeepAlive field to a negative value above, results in OS defaults for |
| 47 | // the TCP keepalive interval and time parameters. |
| 48 | Control: func(_, _ string, c syscall.RawConn) error { |
| 49 | return c.Control(func(fd uintptr) { |
| 50 | windows.SetsockoptInt(windows.Handle(fd), windows.SOL_SOCKET, windows.SO_KEEPALIVE, 1) |
| 51 | }) |
| 52 | }, |
| 53 | } |
| 54 | } |
no outgoing calls
no test coverage detected