client: define a DefaultTransport

This commit is contained in:
Brian Waldon 2015-01-28 12:07:15 -08:00 committed by Yicheng Qin
parent ce4486ff85
commit 8621caf3e2
2 changed files with 20 additions and 3 deletions

View File

@ -18,6 +18,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net"
"net/http" "net/http"
"net/url" "net/url"
"sync" "sync"
@ -41,6 +42,15 @@ var (
DefaultMaxRedirects = 10 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 { type Config struct {
// Endpoints defines a set of URLs (schemes, hosts and ports only) // Endpoints defines a set of URLs (schemes, hosts and ports only)
// that can be used to communicate with a logical etcd cluster. For // that can be used to communicate with a logical etcd cluster. For
@ -60,10 +70,17 @@ type Config struct {
Endpoints []string Endpoints []string
// Transport is used by the Client to drive HTTP requests. If not // 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 Transport CancelableTransport
} }
func (cfg *Config) transport() CancelableTransport {
if cfg.Transport == nil {
return DefaultTransport
}
return cfg.Transport
}
// CancelableTransport mimics net/http.Transport, but requires that // CancelableTransport mimics net/http.Transport, but requires that
// the object also support request cancellation. // the object also support request cancellation.
type CancelableTransport interface { type CancelableTransport interface {
@ -84,7 +101,7 @@ type Client interface {
} }
func New(cfg Config) (Client, error) { 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 { if err := c.reset(cfg.Endpoints); err != nil {
return nil, err return nil, err
} }

View File

@ -26,7 +26,7 @@ Create a Config and exchange it for a Client:
cfg := client.Config{ cfg := client.Config{
Endpoints: []string{"http://127.0.0.1:4001"}, Endpoints: []string{"http://127.0.0.1:4001"},
Transport: http.DefaultTransport, Transport: DefaultTransport,
} }
c, err := client.New(cfg) c, err := client.New(cfg)