Use cleanhttp to get rid of DefaultTransport

This commit is contained in:
Jeff Mitchell 2015-10-22 10:47:50 -04:00
parent aae298a179
commit 06bb9d5f36
5 changed files with 27 additions and 20 deletions

View File

@ -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]
}

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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{