move to newNetDialerFunc

This commit is contained in:
Cooper Oh 2024-07-18 21:43:52 +09:00
parent 29bba1ad6c
commit dc42337df9
2 changed files with 21 additions and 13 deletions

View File

@ -246,19 +246,7 @@ func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader h
defer cancel()
}
var netDial netDialerFunc
switch {
case u.Scheme == "https" && d.NetDialTLSContext != nil:
netDial = d.NetDialTLSContext
case d.NetDialContext != nil:
netDial = d.NetDialContext
case d.NetDial != nil:
netDial = func(ctx context.Context, net, addr string) (net.Conn, error) {
return d.NetDial(net, addr)
}
default:
netDial = (&net.Dialer{}).DialContext
}
netDial := newNetDialerFunc(u.Scheme, d.NetDial, d.NetDialContext, d.NetDialTLSContext)
// If needed, wrap the dial function to connect through a proxy.
if d.Proxy != nil {

View File

@ -16,6 +16,26 @@ import (
"strings"
)
func newNetDialerFunc(
scheme string,
netDial func(network, addr string) (net.Conn, error),
netDialContext func(ctx context.Context, network, addr string) (net.Conn, error),
netDialTLSContext func(ctx context.Context, network, addr string) (net.Conn, error),
) netDialerFunc {
switch {
case scheme == "https" && netDialTLSContext != nil:
return netDialTLSContext
case netDialContext != nil:
return netDialContext
case netDial != nil:
return func(ctx context.Context, net, addr string) (net.Conn, error) {
return netDial(net, addr)
}
default:
return (&net.Dialer{}).DialContext
}
}
type netDialerFunc func(ctx context.Context, network, addr string) (net.Conn, error)
func (fn netDialerFunc) Dial(network, addr string) (net.Conn, error) {