]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: add Transport.Dialer, plumb RoundTrip contexts to net package
authorBrad Fitzpatrick <bradfitz@golang.org>
Fri, 15 Apr 2016 22:59:36 +0000 (22:59 +0000)
committerBrad Fitzpatrick <bradfitz@golang.org>
Sat, 16 Apr 2016 14:07:25 +0000 (14:07 +0000)
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 <iant@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/net/http/transport.go

index 7692abff47433c11973b76b08153173c6c2fb404..05688227374bbe6068cc2d8ab9b7ebac6619f556 100644 (file)
@@ -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)