From 9a5fd5424a85ffd226d1c1fe819d843e3077fb61 Mon Sep 17 00:00:00 2001 From: Jeff Mitchell Date: Thu, 22 Oct 2015 10:47:50 -0400 Subject: [PATCH] Use cleanhttp to get rid of DefaultTransport --- api/api.go | 14 ++++++++------ command/agent/check.go | 5 +++-- command/agent/http_test.go | 11 ++++++----- command/agent/ui_endpoint_test.go | 3 ++- testutil/server.go | 14 ++++++++------ 5 files changed, 27 insertions(+), 20 deletions(-) diff --git a/api/api.go b/api/api.go index 0d248f6553..e76231780b 100644 --- a/api/api.go +++ b/api/api.go @@ -14,6 +14,8 @@ import ( "strconv" "strings" "time" + + "github.com/hashicorp/cleanhttp" ) // QueryOptions are used to parameterize a query @@ -119,7 +121,7 @@ func DefaultConfig() *Config { config := &Config{ Address: "127.0.0.1:8500", Scheme: "http", - HttpClient: &http.Client{}, + HttpClient: cleanhttp.DefaultClient(), } if addr := os.Getenv("CONSUL_HTTP_ADDR"); addr != "" { @@ -198,12 +200,12 @@ func NewClient(config *Config) (*Client, error) { } if parts := strings.SplitN(config.Address, "unix://", 2); len(parts) == 2 { + trans := cleanhttp.DefaultTransport() + trans.Dial = func(_, _ string) (net.Conn, error) { + return net.Dial("unix", parts[1]) + } config.HttpClient = &http.Client{ - Transport: &http.Transport{ - Dial: func(_, _ string) (net.Conn, error) { - return net.Dial("unix", parts[1]) - }, - }, + Transport: trans, } config.Address = parts[1] } diff --git a/command/agent/check.go b/command/agent/check.go index 97ac592eb4..ee0e16d08b 100644 --- a/command/agent/check.go +++ b/command/agent/check.go @@ -12,6 +12,7 @@ import ( "time" "github.com/armon/circbuf" + "github.com/hashicorp/cleanhttp" "github.com/hashicorp/consul/consul/structs" ) @@ -313,13 +314,13 @@ func (c *CheckHTTP) Start() { if c.httpClient == nil { // Create the transport. We disable HTTP Keep-Alive's to prevent // failing checks due to the keepalive interval. - trans := *http.DefaultTransport.(*http.Transport) + trans := cleanhttp.DefaultTransport() trans.DisableKeepAlives = true // Create the HTTP client. c.httpClient = &http.Client{ Timeout: 10 * time.Second, - Transport: &trans, + Transport: trans, } // For long (>10s) interval checks the http timeout is 10s, otherwise the diff --git a/command/agent/http_test.go b/command/agent/http_test.go index 9869b258dd..154007eba9 100644 --- a/command/agent/http_test.go +++ b/command/agent/http_test.go @@ -18,6 +18,7 @@ import ( "testing" "time" + "github.com/hashicorp/cleanhttp" "github.com/hashicorp/consul/consul/structs" "github.com/hashicorp/consul/testutil" ) @@ -95,12 +96,12 @@ func TestHTTPServer_UnixSocket(t *testing.T) { // Ensure we can get a response from the socket. path, _ := unixSocketAddr(srv.agent.config.Addresses.HTTP) + trans := cleanhttp.DefaultTransport() + trans.Dial = func(_, _ string) (net.Conn, error) { + return net.Dial("unix", path) + } client := &http.Client{ - Transport: &http.Transport{ - Dial: func(_, _ string) (net.Conn, error) { - return net.Dial("unix", path) - }, - }, + Transport: trans, } // This URL doesn't look like it makes sense, but the scheme (http://) and diff --git a/command/agent/ui_endpoint_test.go b/command/agent/ui_endpoint_test.go index 1d964d622d..b640776d3b 100644 --- a/command/agent/ui_endpoint_test.go +++ b/command/agent/ui_endpoint_test.go @@ -12,6 +12,7 @@ import ( "reflect" "testing" + "github.com/hashicorp/cleanhttp" "github.com/hashicorp/consul/consul/structs" "github.com/hashicorp/consul/testutil" ) @@ -37,7 +38,7 @@ func TestUiIndex(t *testing.T) { req.URL.Host = srv.listener.Addr().String() // Make the request - client := &http.Client{} + client := cleanhttp.DefaultClient() resp, err := client.Do(req) if err != nil { t.Fatalf("err: %v", err) diff --git a/testutil/server.go b/testutil/server.go index ac77b6c7a4..41b27d86f0 100644 --- a/testutil/server.go +++ b/testutil/server.go @@ -25,6 +25,8 @@ import ( "strings" "sync/atomic" "testing" + + "github.com/hashicorp/cleanhttp" ) // offset is used to atomically increment the port numbers. @@ -191,16 +193,16 @@ func NewTestServerConfig(t *testing.T, cb ServerConfigCallback) *TestServer { var client *http.Client if strings.HasPrefix(consulConfig.Addresses.HTTP, "unix://") { httpAddr = consulConfig.Addresses.HTTP + trans := cleanhttp.DefaultTransport() + trans.Dial = func(_, _ string) (net.Conn, error) { + return net.Dial("unix", httpAddr[7:]) + } client = &http.Client{ - Transport: &http.Transport{ - Dial: func(_, _ string) (net.Conn, error) { - return net.Dial("unix", httpAddr[7:]) - }, - }, + Transport: trans, } } else { httpAddr = fmt.Sprintf("127.0.0.1:%d", consulConfig.Ports.HTTP) - client = &http.Client{} + client = cleanhttp.DefaultClient() } server := &TestServer{