only init one consul client, and lower keepalive
The consul Client is analogous to an http.Client, and we really don't need more than 1. Configure a single client and store it in the backend. Replace the default Transport's Dialer to reduce the KeepAlive setting from 30s to 17s. This avoids racing with the common network timeout value of 30s, and is also coprime to other common intervals.
This commit is contained in:
parent
d0ecb232ae
commit
fd9adcdb36
|
@ -2,7 +2,9 @@ package consul
|
|||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
consulapi "github.com/hashicorp/consul/api"
|
||||
"github.com/hashicorp/terraform/backend"
|
||||
|
@ -100,6 +102,7 @@ type Backend struct {
|
|||
*schema.Backend
|
||||
|
||||
// The fields below are set from configure
|
||||
client *consulapi.Client
|
||||
configData *schema.ResourceData
|
||||
lock bool
|
||||
}
|
||||
|
@ -111,16 +114,18 @@ func (b *Backend) configure(ctx context.Context) error {
|
|||
// Store the lock information
|
||||
b.lock = b.configData.Get("lock").(bool)
|
||||
|
||||
// Initialize a client to test config
|
||||
_, err := b.clientRaw()
|
||||
return err
|
||||
}
|
||||
|
||||
func (b *Backend) clientRaw() (*consulapi.Client, error) {
|
||||
data := b.configData
|
||||
|
||||
// Configure the client
|
||||
config := consulapi.DefaultConfig()
|
||||
|
||||
// replace the default Transport Dialer to reduce the KeepAlive
|
||||
|
||||
config.Transport.DialContext = (&net.Dialer{
|
||||
Timeout: 30 * time.Second,
|
||||
KeepAlive: 17 * time.Second,
|
||||
}).DialContext
|
||||
|
||||
if v, ok := data.GetOk("access_token"); ok && v.(string) != "" {
|
||||
config.Token = v.(string)
|
||||
}
|
||||
|
@ -162,5 +167,11 @@ func (b *Backend) clientRaw() (*consulapi.Client, error) {
|
|||
}
|
||||
}
|
||||
|
||||
return consulapi.NewClient(config)
|
||||
client, err := consulapi.NewClient(config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
b.client = client
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -15,15 +15,9 @@ const (
|
|||
)
|
||||
|
||||
func (b *Backend) States() ([]string, error) {
|
||||
// Get the Consul client
|
||||
client, err := b.clientRaw()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// List our raw path
|
||||
prefix := b.configData.Get("path").(string) + keyEnvPrefix
|
||||
keys, _, err := client.KV().Keys(prefix, "/", nil)
|
||||
keys, _, err := b.client.KV().Keys(prefix, "/", nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -60,28 +54,16 @@ func (b *Backend) DeleteState(name string) error {
|
|||
return fmt.Errorf("can't delete default state")
|
||||
}
|
||||
|
||||
// Get the Consul API client
|
||||
client, err := b.clientRaw()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Determine the path of the data
|
||||
path := b.path(name)
|
||||
|
||||
// Delete it. We just delete it without any locking since
|
||||
// the DeleteState API is documented as such.
|
||||
_, err = client.KV().Delete(path, nil)
|
||||
_, err := b.client.KV().Delete(path, nil)
|
||||
return err
|
||||
}
|
||||
|
||||
func (b *Backend) State(name string) (state.State, error) {
|
||||
// Get the Consul API client
|
||||
client, err := b.clientRaw()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Determine the path of the data
|
||||
path := b.path(name)
|
||||
|
||||
|
@ -91,7 +73,7 @@ func (b *Backend) State(name string) (state.State, error) {
|
|||
// Build the state client
|
||||
var stateMgr state.State = &remote.State{
|
||||
Client: &RemoteClient{
|
||||
Client: client,
|
||||
Client: b.client,
|
||||
Path: path,
|
||||
GZip: gzip,
|
||||
lockState: b.lock,
|
||||
|
|
Loading…
Reference in New Issue