@@ -12,6 +12,7 @@ package http
12
12
import (
13
13
"bufio"
14
14
"compress/gzip"
15
+ "context"
15
16
"crypto/tls"
16
17
"errors"
17
18
"fmt"
@@ -32,10 +33,10 @@ import (
32
33
// $no_proxy) environment variables.
33
34
var DefaultTransport RoundTripper = & Transport {
34
35
Proxy : ProxyFromEnvironment ,
35
- Dial : ( & net.Dialer {
36
+ Dialer : & net.Dialer {
36
37
Timeout : 30 * time .Second ,
37
38
KeepAlive : 30 * time .Second ,
38
- }). Dial ,
39
+ },
39
40
TLSHandshakeTimeout : 10 * time .Second ,
40
41
ExpectContinueTimeout : 1 * time .Second ,
41
42
}
@@ -80,10 +81,17 @@ type Transport struct {
80
81
Proxy func (* Request ) (* url.URL , error )
81
82
82
83
// Dial specifies the dial function for creating unencrypted
83
- // TCP connections.
84
- // If Dial is nil, net.Dial is used.
84
+ // TCP connections. If Dial and Dialer are both nil, net.Dial
85
+ // is used.
86
+ //
87
+ // Deprecated: Use Dialer instead. If both are specified, Dialer
88
+ // takes precedence.
85
89
Dial func (network , addr string ) (net.Conn , error )
86
90
91
+ // Dialer optionally specifies a dialer configuration to use
92
+ // for new connections.
93
+ Dialer * net.Dialer
94
+
87
95
// DialTLS specifies an optional dial function for creating
88
96
// TLS connections for non-proxied HTTPS requests.
89
97
//
@@ -689,7 +697,10 @@ func (t *Transport) replaceReqCanceler(r *Request, fn func()) bool {
689
697
return true
690
698
}
691
699
692
- func (t * Transport ) dial (network , addr string ) (net.Conn , error ) {
700
+ func (t * Transport ) dial (ctx context.Context , network , addr string ) (net.Conn , error ) {
701
+ if t .Dialer != nil {
702
+ return t .Dialer .DialContext (ctx , network , addr )
703
+ }
693
704
if t .Dial != nil {
694
705
c , err := t .Dial (network , addr )
695
706
if c == nil && err == nil {
@@ -705,6 +716,7 @@ func (t *Transport) dial(network, addr string) (net.Conn, error) {
705
716
// and/or setting up TLS. If this doesn't return an error, the persistConn
706
717
// is ready to write requests to.
707
718
func (t * Transport ) getConn (req * Request , cm connectMethod ) (* persistConn , error ) {
719
+ ctx := req .Context ()
708
720
if pc := t .getIdleConn (cm ); pc != nil {
709
721
// set request canceler to some non-nil function so we
710
722
// can detect whether it was cleared between now and when
@@ -738,7 +750,7 @@ func (t *Transport) getConn(req *Request, cm connectMethod) (*persistConn, error
738
750
t .setReqCanceler (req , func () { close (cancelc ) })
739
751
740
752
go func () {
741
- pc , err := t .dialConn (cm )
753
+ pc , err := t .dialConn (ctx , cm )
742
754
dialc <- dialRes {pc , err }
743
755
}()
744
756
@@ -767,7 +779,7 @@ func (t *Transport) getConn(req *Request, cm connectMethod) (*persistConn, error
767
779
}
768
780
}
769
781
770
- func (t * Transport ) dialConn (cm connectMethod ) (* persistConn , error ) {
782
+ func (t * Transport ) dialConn (ctx context. Context , cm connectMethod ) (* persistConn , error ) {
771
783
pconn := & persistConn {
772
784
t : t ,
773
785
cacheKey : cm .key (),
@@ -797,7 +809,7 @@ func (t *Transport) dialConn(cm connectMethod) (*persistConn, error) {
797
809
pconn .tlsState = & cs
798
810
}
799
811
} else {
800
- conn , err := t .dial ("tcp" , cm .addr ())
812
+ conn , err := t .dial (ctx , "tcp" , cm .addr ())
801
813
if err != nil {
802
814
if cm .proxyURL != nil {
803
815
err = fmt .Errorf ("http: error connecting to proxy %s: %v" , cm .proxyURL , err )
0 commit comments