@@ -67,13 +67,23 @@ const (
67
67
// A Transport internally caches connections to servers. It is safe
68
68
// for concurrent use by multiple goroutines.
69
69
type Transport struct {
70
- // DialTLS specifies an optional dial function for creating
71
- // TLS connections for requests.
70
+ // DialTLSContext specifies an optional dial function with context for
71
+ // creating TLS connections for requests.
72
72
//
73
- // If DialTLS is nil, tls.Dial is used.
73
+ // If DialTLSContext and DialTLS is nil, tls.Dial is used.
74
74
//
75
75
// If the returned net.Conn has a ConnectionState method like tls.Conn,
76
76
// it will be used to set http.Response.TLS.
77
+ DialTLSContext func (ctx context.Context , network , addr string , cfg * tls.Config ) (net.Conn , error )
78
+
79
+ // DialTLS specifies an optional dial function for creating
80
+ // TLS connections for requests.
81
+ //
82
+ // If DialTLSContext and DialTLS is nil, tls.Dial is used.
83
+ //
84
+ // Deprecated: Use DialTLSContext instead, which allows the transport
85
+ // to cancel dials as soon as they are no longer needed.
86
+ // If both are set, DialTLSContext takes priority.
77
87
DialTLS func (network , addr string , cfg * tls.Config ) (net.Conn , error )
78
88
79
89
// TLSClientConfig specifies the TLS configuration to use with
@@ -592,7 +602,7 @@ func (t *Transport) dialClientConn(ctx context.Context, addr string, singleUse b
592
602
if err != nil {
593
603
return nil , err
594
604
}
595
- tconn , err := t .dialTLS (ctx )( "tcp" , addr , t .newTLSConfig (host ))
605
+ tconn , err := t .dialTLS (ctx , "tcp" , addr , t .newTLSConfig (host ))
596
606
if err != nil {
597
607
return nil , err
598
608
}
@@ -613,24 +623,25 @@ func (t *Transport) newTLSConfig(host string) *tls.Config {
613
623
return cfg
614
624
}
615
625
616
- func (t * Transport ) dialTLS (ctx context.Context ) func (string , string , * tls.Config ) (net.Conn , error ) {
617
- if t .DialTLS != nil {
618
- return t .DialTLS
626
+ func (t * Transport ) dialTLS (ctx context.Context , network , addr string , tlsCfg * tls.Config ) (net.Conn , error ) {
627
+ if t .DialTLSContext != nil {
628
+ return t .DialTLSContext (ctx , network , addr , tlsCfg )
629
+ } else if t .DialTLS != nil {
630
+ return t .DialTLS (network , addr , tlsCfg )
619
631
}
620
- return func (network , addr string , cfg * tls.Config ) (net.Conn , error ) {
621
- tlsCn , err := t .dialTLSWithContext (ctx , network , addr , cfg )
622
- if err != nil {
623
- return nil , err
624
- }
625
- state := tlsCn .ConnectionState ()
626
- if p := state .NegotiatedProtocol ; p != NextProtoTLS {
627
- return nil , fmt .Errorf ("http2: unexpected ALPN protocol %q; want %q" , p , NextProtoTLS )
628
- }
629
- if ! state .NegotiatedProtocolIsMutual {
630
- return nil , errors .New ("http2: could not negotiate protocol mutually" )
631
- }
632
- return tlsCn , nil
632
+
633
+ tlsCn , err := t .dialTLSWithContext (ctx , network , addr , tlsCfg )
634
+ if err != nil {
635
+ return nil , err
636
+ }
637
+ state := tlsCn .ConnectionState ()
638
+ if p := state .NegotiatedProtocol ; p != NextProtoTLS {
639
+ return nil , fmt .Errorf ("http2: unexpected ALPN protocol %q; want %q" , p , NextProtoTLS )
640
+ }
641
+ if ! state .NegotiatedProtocolIsMutual {
642
+ return nil , errors .New ("http2: could not negotiate protocol mutually" )
633
643
}
644
+ return tlsCn , nil
634
645
}
635
646
636
647
// disableKeepAlives reports whether connections should be closed as
0 commit comments