Update unix dial functions to use DialContext with new go-cleanhttp

This commit is contained in:
Jeff Mitchell 2017-02-10 21:11:21 -05:00
parent 73d8529b3a
commit 44733caf5c
5 changed files with 21 additions and 17 deletions

View File

@ -2,6 +2,7 @@ package api
import ( import (
"bytes" "bytes"
"context"
"crypto/tls" "crypto/tls"
"crypto/x509" "crypto/x509"
"encoding/json" "encoding/json"
@ -354,7 +355,7 @@ func NewClient(config *Config) (*Client, error) {
config.Scheme = "https" config.Scheme = "https"
case "unix": case "unix":
trans := cleanhttp.DefaultTransport() trans := cleanhttp.DefaultTransport()
trans.Dial = func(_, _ string) (net.Conn, error) { trans.DialContext = func(_ context.Context, _, _ string) (net.Conn, error) {
return net.Dial("unix", parts[1]) return net.Dial("unix", parts[1])
} }
config.HttpClient = &http.Client{ config.HttpClient = &http.Client{

View File

@ -42,7 +42,6 @@ func makeClientWithConfig(
if cb1 != nil { if cb1 != nil {
cb1(conf) cb1(conf)
} }
// Create server // Create server
server := testutil.NewTestServerConfig(t, cb2) server := testutil.NewTestServerConfig(t, cb2)
conf.Address = server.HTTPAddr conf.Address = server.HTTPAddr

View File

@ -13,6 +13,7 @@ package testutil
import ( import (
"bytes" "bytes"
"context"
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"fmt" "fmt"
@ -223,7 +224,7 @@ func NewTestServerConfig(t TestingT, cb ServerConfigCallback) *TestServer {
if strings.HasPrefix(consulConfig.Addresses.HTTP, "unix://") { if strings.HasPrefix(consulConfig.Addresses.HTTP, "unix://") {
httpAddr = consulConfig.Addresses.HTTP httpAddr = consulConfig.Addresses.HTTP
trans := cleanhttp.DefaultTransport() trans := cleanhttp.DefaultTransport()
trans.Dial = func(_, _ string) (net.Conn, error) { trans.DialContext = func(_ context.Context, _, _ string) (net.Conn, error) {
return net.Dial("unix", httpAddr[7:]) return net.Dial("unix", httpAddr[7:])
} }
client = &http.Client{ client = &http.Client{

View File

@ -3,11 +3,12 @@ package cleanhttp
import ( import (
"net" "net"
"net/http" "net/http"
"runtime"
"time" "time"
) )
// DefaultTransport returns a new http.Transport with the same default values // DefaultTransport returns a new http.Transport with similar default values to
// as http.DefaultTransport, but with idle connections and keepalives disabled. // http.DefaultTransport, but with idle connections and keepalives disabled.
func DefaultTransport() *http.Transport { func DefaultTransport() *http.Transport {
transport := DefaultPooledTransport() transport := DefaultPooledTransport()
transport.DisableKeepAlives = true transport.DisableKeepAlives = true
@ -22,13 +23,15 @@ func DefaultTransport() *http.Transport {
func DefaultPooledTransport() *http.Transport { func DefaultPooledTransport() *http.Transport {
transport := &http.Transport{ transport := &http.Transport{
Proxy: http.ProxyFromEnvironment, Proxy: http.ProxyFromEnvironment,
Dial: (&net.Dialer{ DialContext: (&net.Dialer{
Timeout: 30 * time.Second, Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second, KeepAlive: 30 * time.Second,
}).Dial, }).DialContext,
TLSHandshakeTimeout: 10 * time.Second, MaxIdleConns: 100,
DisableKeepAlives: false, IdleConnTimeout: 90 * time.Second,
MaxIdleConnsPerHost: 1, TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
MaxIdleConnsPerHost: runtime.GOMAXPROCS(0) + 1,
} }
return transport return transport
} }
@ -42,10 +45,10 @@ func DefaultClient() *http.Client {
} }
} }
// DefaultPooledClient returns a new http.Client with the same default values // DefaultPooledClient returns a new http.Client with similar default values to
// as http.Client, but with a shared Transport. Do not use this function // http.Client, but with a shared Transport. Do not use this function for
// for transient clients as it can leak file descriptors over time. Only use // transient clients as it can leak file descriptors over time. Only use this
// this for clients that will be re-used for the same host(s). // for clients that will be re-used for the same host(s).
func DefaultPooledClient() *http.Client { func DefaultPooledClient() *http.Client {
return &http.Client{ return &http.Client{
Transport: DefaultPooledTransport(), Transport: DefaultPooledTransport(),

6
vendor/vendor.json vendored
View File

@ -438,10 +438,10 @@
"revisionTime": "2015-10-22T18:15:14Z" "revisionTime": "2015-10-22T18:15:14Z"
}, },
{ {
"checksumSHA1": "Uzyon2091lmwacNsl1hCytjhHtg=", "checksumSHA1": "b8F628srIitj5p7Y130xc9k0QWs=",
"path": "github.com/hashicorp/go-cleanhttp", "path": "github.com/hashicorp/go-cleanhttp",
"revision": "ad28ea4487f05916463e2423a55166280e8254b5", "revision": "3573b8b52aa7b37b9358d966a898feb387f62437",
"revisionTime": "2016-04-07T17:41:26Z" "revisionTime": "2017-02-11T01:34:15Z"
}, },
{ {
"checksumSHA1": "jPxyofQxI1PRPq6LPc6VlcRn5fI=", "checksumSHA1": "jPxyofQxI1PRPq6LPc6VlcRn5fI=",