// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package proxy

import (
	
	
)

// A ContextDialer dials using a context.
type ContextDialer interface {
	DialContext(ctx context.Context, network, address string) (net.Conn, error)
}

// Dial works like DialContext on net.Dialer but using a dialer returned by FromEnvironment.
//
// The passed ctx is only used for returning the Conn, not the lifetime of the Conn.
//
// Custom dialers (registered via RegisterDialerType) that do not implement ContextDialer
// can leak a goroutine for as long as it takes the underlying Dialer implementation to timeout.
//
// A Conn returned from a successful Dial after the context has been cancelled will be immediately closed.
func ( context.Context, ,  string) (net.Conn, error) {
	 := FromEnvironment()
	if ,  := .(ContextDialer);  {
		return .DialContext(, , )
	}
	return dialContext(, , , )
}

// WARNING: this can leak a goroutine for as long as the underlying Dialer implementation takes to timeout
// A Conn returned from a successful Dial after the context has been cancelled will be immediately closed.
func ( context.Context,  Dialer, ,  string) (net.Conn, error) {
	var (
		 net.Conn
		 = make(chan struct{}, 1)
		  error
	)
	go func() {
		,  = .Dial(, )
		close()
		if  != nil && .Err() != nil {
			.Close()
		}
	}()
	select {
	case <-.Done():
		 = .Err()
	case <-:
	}
	return , 
}