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 (
"bytes"
"context"
"crypto/tls"
"crypto/x509"
"encoding/json"
@ -354,7 +355,7 @@ func NewClient(config *Config) (*Client, error) {
config.Scheme = "https"
case "unix":
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])
}
config.HttpClient = &http.Client{

View File

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

View File

@ -13,6 +13,7 @@ package testutil
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"fmt"
@ -223,7 +224,7 @@ func NewTestServerConfig(t TestingT, cb ServerConfigCallback) *TestServer {
if strings.HasPrefix(consulConfig.Addresses.HTTP, "unix://") {
httpAddr = consulConfig.Addresses.HTTP
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:])
}
client = &http.Client{

View File

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

6
vendor/vendor.json vendored
View File

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