From: Brad Fitzpatrick Date: Fri, 15 Apr 2016 22:59:36 +0000 (+0000) Subject: net/http: add Transport.Dialer, plumb RoundTrip contexts to net package X-Git-Tag: go1.7beta1~645 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=585590549a3c6e26e7963081e11478a1913744a6;p=gostls13.git 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 Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot --- diff --git a/src/net/http/transport.go b/src/net/http/transport.go index 7692abff47..0568822737 100644 --- a/src/net/http/transport.go +++ b/src/net/http/transport.go @@ -12,6 +12,7 @@ package http import ( "bufio" "compress/gzip" + "context" "crypto/tls" "errors" "fmt" @@ -32,10 +33,10 @@ import ( // $no_proxy) environment variables. var DefaultTransport RoundTripper = &Transport{ Proxy: ProxyFromEnvironment, - Dial: (&net.Dialer{ + Dialer: &net.Dialer{ Timeout: 30 * time.Second, KeepAlive: 30 * time.Second, - }).Dial, + }, TLSHandshakeTimeout: 10 * time.Second, ExpectContinueTimeout: 1 * time.Second, } @@ -80,10 +81,17 @@ type Transport struct { Proxy func(*Request) (*url.URL, error) // Dial specifies the dial function for creating unencrypted - // TCP connections. - // If Dial is nil, net.Dial is used. + // TCP connections. If Dial and Dialer are both nil, net.Dial + // is used. + // + // Deprecated: Use Dialer instead. If both are specified, Dialer + // takes precedence. Dial func(network, addr string) (net.Conn, error) + // Dialer optionally specifies a dialer configuration to use + // for new connections. + Dialer *net.Dialer + // DialTLS specifies an optional dial function for creating // TLS connections for non-proxied HTTPS requests. // @@ -689,7 +697,10 @@ func (t *Transport) replaceReqCanceler(r *Request, fn func()) bool { return true } -func (t *Transport) dial(network, addr string) (net.Conn, error) { +func (t *Transport) dial(ctx context.Context, network, addr string) (net.Conn, error) { + if t.Dialer != nil { + return t.Dialer.DialContext(ctx, network, addr) + } if t.Dial != nil { c, err := t.Dial(network, addr) if c == nil && err == nil { @@ -705,6 +716,7 @@ func (t *Transport) dial(network, addr string) (net.Conn, error) { // and/or setting up TLS. If this doesn't return an error, the persistConn // is ready to write requests to. func (t *Transport) getConn(req *Request, cm connectMethod) (*persistConn, error) { + ctx := req.Context() if pc := t.getIdleConn(cm); pc != nil { // set request canceler to some non-nil function so we // can detect whether it was cleared between now and when @@ -738,7 +750,7 @@ func (t *Transport) getConn(req *Request, cm connectMethod) (*persistConn, error t.setReqCanceler(req, func() { close(cancelc) }) go func() { - pc, err := t.dialConn(cm) + pc, err := t.dialConn(ctx, cm) dialc <- dialRes{pc, err} }() @@ -767,7 +779,7 @@ func (t *Transport) getConn(req *Request, cm connectMethod) (*persistConn, error } } -func (t *Transport) dialConn(cm connectMethod) (*persistConn, error) { +func (t *Transport) dialConn(ctx context.Context, cm connectMethod) (*persistConn, error) { pconn := &persistConn{ t: t, cacheKey: cm.key(), @@ -797,7 +809,7 @@ func (t *Transport) dialConn(cm connectMethod) (*persistConn, error) { pconn.tlsState = &cs } } else { - conn, err := t.dial("tcp", cm.addr()) + conn, err := t.dial(ctx, "tcp", cm.addr()) if err != nil { if cm.proxyURL != nil { err = fmt.Errorf("http: error connecting to proxy %s: %v", cm.proxyURL, err)