Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

phys/consul: Allow tuning of session ttl and lock wait time #4352

Merged
merged 5 commits into from
Apr 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion physical/consul/consul.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ type ConsulBackend struct {

notifyActiveCh chan notifyEvent
notifySealedCh chan notifyEvent

sessionTTL string
lockWaitTime time.Duration
}

// NewConsulBackend constructs a Consul backend using the given API client
Expand Down Expand Up @@ -168,7 +171,7 @@ func NewConsulBackend(conf map[string]string, logger log.Logger) (physical.Backe
checkTimeout := defaultCheckTimeout
checkTimeoutStr, ok := conf["check_timeout"]
if ok {
d, err := time.ParseDuration(checkTimeoutStr)
d, err := parseutil.ParseDurationSecond(checkTimeoutStr)
if err != nil {
return nil, err
}
Expand All @@ -184,6 +187,32 @@ func NewConsulBackend(conf map[string]string, logger log.Logger) (physical.Backe
}
}

sessionTTL := api.DefaultLockSessionTTL
sessionTTLStr, ok := conf["session_ttl"]
if ok {
_, err := parseutil.ParseDurationSecond(sessionTTLStr)
if err != nil {
return nil, errwrap.Wrapf("invalid session_ttl: {{err}}", err)
}
sessionTTL = sessionTTLStr
if logger.IsDebug() {
logger.Debug("config session_ttl set", "session_ttl", sessionTTL)
}
}

lockWaitTime := api.DefaultLockWaitTime
lockWaitTimeRaw, ok := conf["lock_wait_time"]
if ok {
d, err := parseutil.ParseDurationSecond(lockWaitTimeRaw)
if err != nil {
return nil, errwrap.Wrapf("invalid lock_wait_time: {{err}}", err)
}
lockWaitTime = d
if logger.IsDebug() {
logger.Debug("config lock_wait_time set", "lock_wait_time", d)
}
}

// Configure the client
consulConf := api.DefaultConfig()
// Set MaxIdleConnsPerHost to the number of processes used in expiration.Restore
Expand Down Expand Up @@ -263,6 +292,8 @@ func NewConsulBackend(conf map[string]string, logger log.Logger) (physical.Backe
consistencyMode: consistencyMode,
notifyActiveCh: make(chan notifyEvent),
notifySealedCh: make(chan notifyEvent),
sessionTTL: sessionTTL,
lockWaitTime: lockWaitTime,
}
return c, nil
}
Expand Down Expand Up @@ -466,6 +497,8 @@ func (c *ConsulBackend) LockWith(key, value string) (physical.Lock, error) {
Value: []byte(value),
SessionName: "Vault Lock",
MonitorRetries: 5,
SessionTTL: c.sessionTTL,
LockWaitTime: c.lockWaitTime,
}
lock, err := c.client.LockOpts(opts)
if err != nil {
Expand Down
11 changes: 11 additions & 0 deletions website/source/docs/configuration/storage/consul.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ at Consul's service discovery layer.
permission to read and write from the `path` in Consul's key-value store.
This is **not** a Vault token. See the ACL section below for help.

- `session_ttl` `(string: "15s")` - Specifies the minimum allowed [session
TTL][consul-session-ttl]. Consul server has a lower limit of 10s on the
session TTL by default. The value of `session_ttl` here cannot be lesser than
10s unless the `session_ttl_min` on the consul server's configuration has a
lesser value.

- `lock_wait_time` `(string: "15s")` - Specifies the wait time before a lock
lock acquisition is made. This affects the minimum time it takes to cancel a
lock acquisition.

The following settings apply when communicating with Consul via an encrypted
connection. You can read more about encrypting Consul connections on the
[Consul encryption page][consul-encryption].
Expand Down Expand Up @@ -225,3 +235,4 @@ storage "consul" {
[consul-consistency]: https://www.consul.io/api/index.html#consistency-modes "Consul Consistency Modes"
[consul-encryption]: https://www.consul.io/docs/agent/encryption.html "Consul Encryption"
[consul-translate-wan-addrs]: https://www.consul.io/docs/agent/options.html#translate_wan_addrs "Consul Configuration"
[consul-session-ttl]: https://www.consul.io/docs/agent/options.html#session_ttl_min "Consul Configuration"