From 8621caf3e2f5c2688abc22c3a5087609f9d2ad2d Mon Sep 17 00:00:00 2001 From: Brian Waldon Date: Wed, 28 Jan 2015 12:07:15 -0800 Subject: [PATCH] client: define a DefaultTransport --- client/client.go | 21 +++++++++++++++++++-- client/doc.go | 2 +- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/client/client.go b/client/client.go index 205d84a0d..73780a290 100644 --- a/client/client.go +++ b/client/client.go @@ -18,6 +18,7 @@ import ( "errors" "fmt" "io/ioutil" + "net" "net/http" "net/url" "sync" @@ -41,6 +42,15 @@ var ( DefaultMaxRedirects = 10 ) +var DefaultTransport CancelableTransport = &http.Transport{ + Proxy: http.ProxyFromEnvironment, + Dial: (&net.Dialer{ + Timeout: 30 * time.Second, + KeepAlive: 30 * time.Second, + }).Dial, + TLSHandshakeTimeout: 10 * time.Second, +} + type Config struct { // Endpoints defines a set of URLs (schemes, hosts and ports only) // that can be used to communicate with a logical etcd cluster. For @@ -60,10 +70,17 @@ type Config struct { Endpoints []string // Transport is used by the Client to drive HTTP requests. If not - // provided, net/http.DefaultTransport will be used. + // provided, DefaultTransport will be used. Transport CancelableTransport } +func (cfg *Config) transport() CancelableTransport { + if cfg.Transport == nil { + return DefaultTransport + } + return cfg.Transport +} + // CancelableTransport mimics net/http.Transport, but requires that // the object also support request cancellation. type CancelableTransport interface { @@ -84,7 +101,7 @@ type Client interface { } func New(cfg Config) (Client, error) { - c := &httpClusterClient{clientFactory: newHTTPClientFactory(cfg.Transport)} + c := &httpClusterClient{clientFactory: newHTTPClientFactory(cfg.transport())} if err := c.reset(cfg.Endpoints); err != nil { return nil, err } diff --git a/client/doc.go b/client/doc.go index f4f14b79a..1a8c8f9f5 100644 --- a/client/doc.go +++ b/client/doc.go @@ -26,7 +26,7 @@ Create a Config and exchange it for a Client: cfg := client.Config{ Endpoints: []string{"http://127.0.0.1:4001"}, - Transport: http.DefaultTransport, + Transport: DefaultTransport, } c, err := client.New(cfg)