add a option for strong consistancy for consul

This commit is contained in:
Brian Kassouf 2017-01-13 09:49:04 -08:00
parent fc1d75032b
commit 2baf39f050

View File

@ -67,6 +67,7 @@ type ConsulBackend struct {
serviceTags []string
disableRegistration bool
checkTimeout time.Duration
requireConsistent bool
notifyActiveCh chan notifyEvent
notifySealedCh chan notifyEvent
@ -193,6 +194,16 @@ func newConsulBackend(conf map[string]string, logger log.Logger) (Backend, error
}
}
requireConsistentRaw, ok := conf["require_consistent"]
var requireConsistent bool
if ok {
b, err := strconv.ParseBool(requireConsistentRaw)
if err != nil {
return nil, errwrap.Wrapf("failed parsing require_consistent parameter: {{err}}", err)
}
requireConsistent = b
}
// Setup the backend
c := &ConsulBackend{
path: path,
@ -204,6 +215,7 @@ func newConsulBackend(conf map[string]string, logger log.Logger) (Backend, error
serviceTags: strutil.ParseDedupAndSortStrings(tags, ","),
checkTimeout: checkTimeout,
disableRegistration: disableRegistration,
requireConsistent: requireConsistent,
}
return c, nil
}
@ -285,7 +297,14 @@ func (c *ConsulBackend) Get(key string) (*Entry, error) {
c.permitPool.Acquire()
defer c.permitPool.Release()
pair, _, err := c.kv.Get(c.path+key, nil)
var queryOptions *api.QueryOptions
if c.requireConsistent {
queryOptions = &api.QueryOptions{
RequireConsistent: c.requireConsistent,
}
}
pair, _, err := c.kv.Get(c.path+key, queryOptions)
if err != nil {
return nil, err
}