Skip to content

Commit

Permalink
net/http: add Transport.Dialer, plumb RoundTrip contexts to net package
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
bradfitz committed Apr 16, 2016
1 parent 318da8d commit 5855905
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/net/http/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package http
import (
"bufio"
"compress/gzip"
"context"
"crypto/tls"
"errors"
"fmt"
Expand All @@ -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,
}
Expand Down Expand Up @@ -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.
//
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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}
}()

Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 5855905

Please sign in to comment.