Use cleanhttp to get rid of DefaultTransport

This commit is contained in:
Jeff Mitchell 2015-10-22 10:47:50 -04:00
parent 158eabdd6f
commit 9a5fd5424a
5 changed files with 27 additions and 20 deletions

View File

@ -14,6 +14,8 @@ import (
"strconv" "strconv"
"strings" "strings"
"time" "time"
"github.com/hashicorp/cleanhttp"
) )
// QueryOptions are used to parameterize a query // QueryOptions are used to parameterize a query
@ -119,7 +121,7 @@ func DefaultConfig() *Config {
config := &Config{ config := &Config{
Address: "127.0.0.1:8500", Address: "127.0.0.1:8500",
Scheme: "http", Scheme: "http",
HttpClient: &http.Client{}, HttpClient: cleanhttp.DefaultClient(),
} }
if addr := os.Getenv("CONSUL_HTTP_ADDR"); addr != "" { 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 { 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{ config.HttpClient = &http.Client{
Transport: &http.Transport{ Transport: trans,
Dial: func(_, _ string) (net.Conn, error) {
return net.Dial("unix", parts[1])
},
},
} }
config.Address = parts[1] config.Address = parts[1]
} }

View File

@ -12,6 +12,7 @@ import (
"time" "time"
"github.com/armon/circbuf" "github.com/armon/circbuf"
"github.com/hashicorp/cleanhttp"
"github.com/hashicorp/consul/consul/structs" "github.com/hashicorp/consul/consul/structs"
) )
@ -313,13 +314,13 @@ func (c *CheckHTTP) Start() {
if c.httpClient == nil { if c.httpClient == nil {
// Create the transport. We disable HTTP Keep-Alive's to prevent // Create the transport. We disable HTTP Keep-Alive's to prevent
// failing checks due to the keepalive interval. // failing checks due to the keepalive interval.
trans := *http.DefaultTransport.(*http.Transport) trans := cleanhttp.DefaultTransport()
trans.DisableKeepAlives = true trans.DisableKeepAlives = true
// Create the HTTP client. // Create the HTTP client.
c.httpClient = &http.Client{ c.httpClient = &http.Client{
Timeout: 10 * time.Second, Timeout: 10 * time.Second,
Transport: &trans, Transport: trans,
} }
// For long (>10s) interval checks the http timeout is 10s, otherwise the // For long (>10s) interval checks the http timeout is 10s, otherwise the

View File

@ -18,6 +18,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/hashicorp/cleanhttp"
"github.com/hashicorp/consul/consul/structs" "github.com/hashicorp/consul/consul/structs"
"github.com/hashicorp/consul/testutil" "github.com/hashicorp/consul/testutil"
) )
@ -95,12 +96,12 @@ func TestHTTPServer_UnixSocket(t *testing.T) {
// Ensure we can get a response from the socket. // Ensure we can get a response from the socket.
path, _ := unixSocketAddr(srv.agent.config.Addresses.HTTP) 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{ client := &http.Client{
Transport: &http.Transport{ Transport: trans,
Dial: func(_, _ string) (net.Conn, error) {
return net.Dial("unix", path)
},
},
} }
// This URL doesn't look like it makes sense, but the scheme (http://) and // This URL doesn't look like it makes sense, but the scheme (http://) and

View File

@ -12,6 +12,7 @@ import (
"reflect" "reflect"
"testing" "testing"
"github.com/hashicorp/cleanhttp"
"github.com/hashicorp/consul/consul/structs" "github.com/hashicorp/consul/consul/structs"
"github.com/hashicorp/consul/testutil" "github.com/hashicorp/consul/testutil"
) )
@ -37,7 +38,7 @@ func TestUiIndex(t *testing.T) {
req.URL.Host = srv.listener.Addr().String() req.URL.Host = srv.listener.Addr().String()
// Make the request // Make the request
client := &http.Client{} client := cleanhttp.DefaultClient()
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)

View File

@ -25,6 +25,8 @@ import (
"strings" "strings"
"sync/atomic" "sync/atomic"
"testing" "testing"
"github.com/hashicorp/cleanhttp"
) )
// offset is used to atomically increment the port numbers. // 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 var client *http.Client
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.Dial = func(_, _ string) (net.Conn, error) {
return net.Dial("unix", httpAddr[7:])
}
client = &http.Client{ client = &http.Client{
Transport: &http.Transport{ Transport: trans,
Dial: func(_, _ string) (net.Conn, error) {
return net.Dial("unix", httpAddr[7:])
},
},
} }
} else { } else {
httpAddr = fmt.Sprintf("127.0.0.1:%d", consulConfig.Ports.HTTP) httpAddr = fmt.Sprintf("127.0.0.1:%d", consulConfig.Ports.HTTP)
client = &http.Client{} client = cleanhttp.DefaultClient()
} }
server := &TestServer{ server := &TestServer{