mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
client: define a DefaultTransport
This commit is contained in:
parent
ce4486ff85
commit
8621caf3e2
@ -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
|
||||||
}
|
}
|
||||||
|
@ -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)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user