// dial with context and timeouts func dialContextTimeout(ctx context.Context, network, address string, connectTimeout, timeout time.Duration) (net.Conn, error) { dialer := net.Dialer{ Timeout: connectTimeout, KeepAlive: 30 * time.Second, } c, err := dialer.DialContext(ctx, network, address) if err != nil { return c, err } return newTimeoutConn(c, timeout), nil }