mirror of https://github.com/hashicorp/consul
Switches default for API client to pooled connections.
parent
3c1aaa3d82
commit
abacccd6e5
28
api/api.go
28
api/api.go
|
@ -122,12 +122,34 @@ type Config struct {
|
||||||
Token string
|
Token string
|
||||||
}
|
}
|
||||||
|
|
||||||
// DefaultConfig returns a default configuration for the client
|
// DefaultConfig returns a default configuration for the client. By default this
|
||||||
|
// will pool and reuse idle connections to Consul. If you have a long-lived
|
||||||
|
// client object, this is the desired behavior and should make the most efficient
|
||||||
|
// use of the connections to Consul. If you don't reuse a client object , which
|
||||||
|
// is not recommended, then you may notice idle connections building up over
|
||||||
|
// time. To avoid this, use the DefaultNonPooledConfig() instead.
|
||||||
func DefaultConfig() *Config {
|
func DefaultConfig() *Config {
|
||||||
|
return defaultConfig(cleanhttp.DefaultPooledTransport)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DefaultNonPooledConfig returns a default configuration for the client which
|
||||||
|
// does not pool connections. This isn't a recommended configuration because it
|
||||||
|
// will reconnect to Consul on every request, but this is useful to avoid the
|
||||||
|
// accumulation of idle connections if you make many client objects during the
|
||||||
|
// lifetime of your application.
|
||||||
|
func DefaultNonPooledConfig() *Config {
|
||||||
|
return defaultConfig(cleanhttp.DefaultTransport)
|
||||||
|
}
|
||||||
|
|
||||||
|
// defaultConfig returns the default configuration for the client, using the
|
||||||
|
// given function to make the transport.
|
||||||
|
func defaultConfig(transportFn func() *http.Transport) *Config {
|
||||||
config := &Config{
|
config := &Config{
|
||||||
Address: "127.0.0.1:8500",
|
Address: "127.0.0.1:8500",
|
||||||
Scheme: "http",
|
Scheme: "http",
|
||||||
HttpClient: cleanhttp.DefaultClient(),
|
HttpClient: &http.Client{
|
||||||
|
Transport: transportFn(),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
if addr := os.Getenv("CONSUL_HTTP_ADDR"); addr != "" {
|
if addr := os.Getenv("CONSUL_HTTP_ADDR"); addr != "" {
|
||||||
|
@ -172,7 +194,7 @@ func DefaultConfig() *Config {
|
||||||
}
|
}
|
||||||
|
|
||||||
if !doVerify {
|
if !doVerify {
|
||||||
transport := cleanhttp.DefaultTransport()
|
transport := transportFn()
|
||||||
transport.TLSClientConfig = &tls.Config{
|
transport.TLSClientConfig = &tls.Config{
|
||||||
InsecureSkipVerify: true,
|
InsecureSkipVerify: true,
|
||||||
}
|
}
|
||||||
|
|
|
@ -85,16 +85,13 @@ func TestDefaultConfig_env(t *testing.T) {
|
||||||
os.Setenv("CONSUL_HTTP_SSL_VERIFY", "0")
|
os.Setenv("CONSUL_HTTP_SSL_VERIFY", "0")
|
||||||
defer os.Setenv("CONSUL_HTTP_SSL_VERIFY", "")
|
defer os.Setenv("CONSUL_HTTP_SSL_VERIFY", "")
|
||||||
|
|
||||||
config := DefaultConfig()
|
for i, config := range []*Config{DefaultConfig(), DefaultNonPooledConfig()} {
|
||||||
|
|
||||||
if config.Address != addr {
|
if config.Address != addr {
|
||||||
t.Errorf("expected %q to be %q", config.Address, addr)
|
t.Errorf("expected %q to be %q", config.Address, addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
if config.Token != token {
|
if config.Token != token {
|
||||||
t.Errorf("expected %q to be %q", config.Token, token)
|
t.Errorf("expected %q to be %q", config.Token, token)
|
||||||
}
|
}
|
||||||
|
|
||||||
if config.HttpAuth == nil {
|
if config.HttpAuth == nil {
|
||||||
t.Fatalf("expected HttpAuth to be enabled")
|
t.Fatalf("expected HttpAuth to be enabled")
|
||||||
}
|
}
|
||||||
|
@ -104,14 +101,24 @@ func TestDefaultConfig_env(t *testing.T) {
|
||||||
if config.HttpAuth.Password != "password" {
|
if config.HttpAuth.Password != "password" {
|
||||||
t.Errorf("expected %q to be %q", config.HttpAuth.Password, "password")
|
t.Errorf("expected %q to be %q", config.HttpAuth.Password, "password")
|
||||||
}
|
}
|
||||||
|
|
||||||
if config.Scheme != "https" {
|
if config.Scheme != "https" {
|
||||||
t.Errorf("expected %q to be %q", config.Scheme, "https")
|
t.Errorf("expected %q to be %q", config.Scheme, "https")
|
||||||
}
|
}
|
||||||
|
|
||||||
if !config.HttpClient.Transport.(*http.Transport).TLSClientConfig.InsecureSkipVerify {
|
if !config.HttpClient.Transport.(*http.Transport).TLSClientConfig.InsecureSkipVerify {
|
||||||
t.Errorf("expected SSL verification to be off")
|
t.Errorf("expected SSL verification to be off")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Use keep alives as a check for whether pooling is on or off.
|
||||||
|
if pooled := i == 0; pooled {
|
||||||
|
if config.HttpClient.Transport.(*http.Transport).DisableKeepAlives != false {
|
||||||
|
t.Errorf("expected keep alives to be enabled")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if config.HttpClient.Transport.(*http.Transport).DisableKeepAlives != true {
|
||||||
|
t.Errorf("expected keep alives to be disabled")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSetQueryOptions(t *testing.T) {
|
func TestSetQueryOptions(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue