Skip to content

Commit 5855905

Browse files
committed
net/http: add Transport.Dialer, plumb RoundTrip contexts to net package
This simply connects the contexts, pushing them down the call stack. Future CLs will utilize them. For #12580 (http.Transport tracing/analytics) Updates #13021 Change-Id: I5b2074d6eb1e87d79a767fc0609c84e7928d1a16 Reviewed-on: https://go-review.googlesource.com/22124 Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 318da8d commit 5855905

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

src/net/http/transport.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ package http
1212
import (
1313
"bufio"
1414
"compress/gzip"
15+
"context"
1516
"crypto/tls"
1617
"errors"
1718
"fmt"
@@ -32,10 +33,10 @@ import (
3233
// $no_proxy) environment variables.
3334
var DefaultTransport RoundTripper = &Transport{
3435
Proxy: ProxyFromEnvironment,
35-
Dial: (&net.Dialer{
36+
Dialer: &net.Dialer{
3637
Timeout: 30 * time.Second,
3738
KeepAlive: 30 * time.Second,
38-
}).Dial,
39+
},
3940
TLSHandshakeTimeout: 10 * time.Second,
4041
ExpectContinueTimeout: 1 * time.Second,
4142
}
@@ -80,10 +81,17 @@ type Transport struct {
8081
Proxy func(*Request) (*url.URL, error)
8182

8283
// 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.
8589
Dial func(network, addr string) (net.Conn, error)
8690

91+
// Dialer optionally specifies a dialer configuration to use
92+
// for new connections.
93+
Dialer *net.Dialer
94+
8795
// DialTLS specifies an optional dial function for creating
8896
// TLS connections for non-proxied HTTPS requests.
8997
//
@@ -689,7 +697,10 @@ func (t *Transport) replaceReqCanceler(r *Request, fn func()) bool {
689697
return true
690698
}
691699

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+
}
693704
if t.Dial != nil {
694705
c, err := t.Dial(network, addr)
695706
if c == nil && err == nil {
@@ -705,6 +716,7 @@ func (t *Transport) dial(network, addr string) (net.Conn, error) {
705716
// and/or setting up TLS. If this doesn't return an error, the persistConn
706717
// is ready to write requests to.
707718
func (t *Transport) getConn(req *Request, cm connectMethod) (*persistConn, error) {
719+
ctx := req.Context()
708720
if pc := t.getIdleConn(cm); pc != nil {
709721
// set request canceler to some non-nil function so we
710722
// can detect whether it was cleared between now and when
@@ -738,7 +750,7 @@ func (t *Transport) getConn(req *Request, cm connectMethod) (*persistConn, error
738750
t.setReqCanceler(req, func() { close(cancelc) })
739751

740752
go func() {
741-
pc, err := t.dialConn(cm)
753+
pc, err := t.dialConn(ctx, cm)
742754
dialc <- dialRes{pc, err}
743755
}()
744756

@@ -767,7 +779,7 @@ func (t *Transport) getConn(req *Request, cm connectMethod) (*persistConn, error
767779
}
768780
}
769781

770-
func (t *Transport) dialConn(cm connectMethod) (*persistConn, error) {
782+
func (t *Transport) dialConn(ctx context.Context, cm connectMethod) (*persistConn, error) {
771783
pconn := &persistConn{
772784
t: t,
773785
cacheKey: cm.key(),
@@ -797,7 +809,7 @@ func (t *Transport) dialConn(cm connectMethod) (*persistConn, error) {
797809
pconn.tlsState = &cs
798810
}
799811
} else {
800-
conn, err := t.dial("tcp", cm.addr())
812+
conn, err := t.dial(ctx, "tcp", cm.addr())
801813
if err != nil {
802814
if cm.proxyURL != nil {
803815
err = fmt.Errorf("http: error connecting to proxy %s: %v", cm.proxyURL, err)

0 commit comments

Comments
 (0)