Skip to content

Commit

Permalink
Moves stale code to same spot and adds it everywhere that supports it.
Browse files Browse the repository at this point in the history
  • Loading branch information
slackpad committed Aug 10, 2016
1 parent 2b65e7d commit 888f27d
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions watch/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,18 @@ func init() {

// keyWatch is used to return a key watching function
func keyWatch(params map[string]interface{}) (WatchFunc, error) {
stale := false
if err := assignValueBool(params, "stale", &stale); err != nil {
return nil, err
}

var key string
if err := assignValue(params, "key", &key); err != nil {
return nil, err
}
if key == "" {
return nil, fmt.Errorf("Must specify a single key to watch")
}

stale := false
if err := assignValueBool(params, "stale", &stale); err != nil {
return nil, err
}

fn := func(p *WatchPlan) (uint64, interface{}, error) {
kv := p.client.KV()
opts := consulapi.QueryOptions{AllowStale: stale, WaitIndex: p.lastIndex}
Expand All @@ -57,19 +56,18 @@ func keyWatch(params map[string]interface{}) (WatchFunc, error) {

// keyPrefixWatch is used to return a key prefix watching function
func keyPrefixWatch(params map[string]interface{}) (WatchFunc, error) {
stale := false
if err := assignValueBool(params, "stale", &stale); err != nil {
return nil, err
}

var prefix string
if err := assignValue(params, "prefix", &prefix); err != nil {
return nil, err
}
if prefix == "" {
return nil, fmt.Errorf("Must specify a single prefix to watch")
}

stale := false
if err := assignValueBool(params, "stale", &stale); err != nil {
return nil, err
}

fn := func(p *WatchPlan) (uint64, interface{}, error) {
kv := p.client.KV()
opts := consulapi.QueryOptions{AllowStale: stale, WaitIndex: p.lastIndex}
Expand Down Expand Up @@ -122,6 +120,11 @@ func nodesWatch(params map[string]interface{}) (WatchFunc, error) {

// serviceWatch is used to watch a specific service for changes
func serviceWatch(params map[string]interface{}) (WatchFunc, error) {
stale := false
if err := assignValueBool(params, "stale", &stale); err != nil {
return nil, err
}

var service, tag string
if err := assignValue(params, "service", &service); err != nil {
return nil, err
Expand All @@ -139,11 +142,6 @@ func serviceWatch(params map[string]interface{}) (WatchFunc, error) {
return nil, err
}

stale := false
if err := assignValueBool(params, "stale", &stale); err != nil {
return nil, err
}

fn := func(p *WatchPlan) (uint64, interface{}, error) {
health := p.client.Health()
opts := consulapi.QueryOptions{AllowStale: stale, WaitIndex: p.lastIndex}
Expand All @@ -158,6 +156,11 @@ func serviceWatch(params map[string]interface{}) (WatchFunc, error) {

// checksWatch is used to watch a specific checks in a given state
func checksWatch(params map[string]interface{}) (WatchFunc, error) {
stale := false
if err := assignValueBool(params, "stale", &stale); err != nil {
return nil, err
}

var service, state string
if err := assignValue(params, "service", &service); err != nil {
return nil, err
Expand All @@ -174,7 +177,7 @@ func checksWatch(params map[string]interface{}) (WatchFunc, error) {

fn := func(p *WatchPlan) (uint64, interface{}, error) {
health := p.client.Health()
opts := consulapi.QueryOptions{WaitIndex: p.lastIndex}
opts := consulapi.QueryOptions{AllowStale: stale, WaitIndex: p.lastIndex}
var checks []*consulapi.HealthCheck
var meta *consulapi.QueryMeta
var err error
Expand All @@ -193,6 +196,8 @@ func checksWatch(params map[string]interface{}) (WatchFunc, error) {

// eventWatch is used to watch for events, optionally filtering on name
func eventWatch(params map[string]interface{}) (WatchFunc, error) {
// The stale setting doesn't apply to events.

var name string
if err := assignValue(params, "name", &name); err != nil {
return nil, err
Expand Down

0 comments on commit 888f27d

Please sign in to comment.