DialFast 实现简化版的 Happy Eyeballs,并记录拨号延迟。
(addr string)
| 17 | return |
| 18 | } |
| 19 | _ = tcpConn.SetNoDelay(true) |
| 20 | _ = tcpConn.SetKeepAlive(true) |
| 21 | _ = tcpConn.SetKeepAlivePeriod(30 * time.Second) |
| 22 | } |
| 23 | |
| 24 | // DialFastContext dials addr with the supplied cancellation context. Go's |
| 25 | // net.Dialer already implements IPv4/IPv6 fallback, avoiding resolver and |
| 26 | // fallback goroutine leaks from a hand-rolled race. |
| 27 | func DialFastContext(ctx context.Context, addr string) (net.Conn, error) { |
| 28 | if ctx == nil { |
| 29 | ctx = context.Background() |
| 30 | } |
| 31 | dialer := &net.Dialer{ |
| 32 | Timeout: dialTimeout, |
| 33 | KeepAlive: 30 * time.Second, |
| 34 | } |
| 35 | conn, err := dialer.DialContext(ctx, "tcp", addr) |
| 36 | if err != nil { |
| 37 | return nil, err |
| 38 | } |
| 39 | configureTCP(conn) |
| 40 | return conn, nil |
| 41 | } |
| 42 | |
| 43 | // DialFast preserves the original API for callers that do not yet carry a |
| 44 | // context. The wrapper remains bounded and delegates to DialFastContext. |
| 45 | func DialFast(addr string) (net.Conn, error) { |
| 46 | ctx, cancel := context.WithTimeout(context.Background(), dialTimeout) |
| 47 | defer cancel() |
| 48 | return DialFastContext(ctx, addr) |
| 49 | } |
no outgoing calls
no test coverage detected