import (
"bufio"
"compress/gzip"
+ "context"
"crypto/tls"
"errors"
"fmt"
// $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,
}
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.
//
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 {
// 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
t.setReqCanceler(req, func() { close(cancelc) })
go func() {
- pc, err := t.dialConn(cm)
+ pc, err := t.dialConn(ctx, cm)
dialc <- dialRes{pc, err}
}()
}
}
-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(),
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)