Merge pull request #1241 from vholovko/healthcheck_changes
using more sensible consul blocking query to detect health check changes
This commit is contained in:
commit
ba928dd459
1 changed files with 19 additions and 4 deletions
|
@ -78,11 +78,14 @@ func (p *CatalogProvider) watchServices(stopCh <-chan struct{}) <-chan map[strin
|
||||||
watchCh := make(chan map[string][]string)
|
watchCh := make(chan map[string][]string)
|
||||||
|
|
||||||
catalog := p.client.Catalog()
|
catalog := p.client.Catalog()
|
||||||
|
health := p.client.Health()
|
||||||
|
var lastHealthIndex uint64
|
||||||
|
|
||||||
safe.Go(func() {
|
safe.Go(func() {
|
||||||
defer close(watchCh)
|
defer close(watchCh)
|
||||||
|
|
||||||
opts := &api.QueryOptions{WaitTime: DefaultWatchWaitTime}
|
catalogOptions := &api.QueryOptions{WaitTime: DefaultWatchWaitTime}
|
||||||
|
healthOptions := &api.QueryOptions{}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
|
@ -91,18 +94,30 @@ func (p *CatalogProvider) watchServices(stopCh <-chan struct{}) <-chan map[strin
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
data, meta, err := catalog.Services(opts)
|
data, catalogMeta, err := catalog.Services(catalogOptions)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithError(err).Errorf("Failed to list services")
|
log.WithError(err).Errorf("Failed to list services")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Listening to changes that leads to `passing` state or degrades from it.
|
||||||
|
// The call is used just as a trigger for further actions
|
||||||
|
// (intentionally there is no interest in the received data).
|
||||||
|
_, healthMeta, err := health.State("passing", healthOptions)
|
||||||
|
if err != nil {
|
||||||
|
log.WithError(err).Errorf("Failed to retrieve health checks")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// If LastIndex didn't change then it means `Get` returned
|
// If LastIndex didn't change then it means `Get` returned
|
||||||
// because of the WaitTime and the key didn't changed.
|
// because of the WaitTime and the key didn't changed.
|
||||||
if opts.WaitIndex == meta.LastIndex {
|
sameServiceAmount := catalogOptions.WaitIndex == catalogMeta.LastIndex
|
||||||
|
sameServiceHealth := lastHealthIndex == healthMeta.LastIndex
|
||||||
|
if sameServiceAmount && sameServiceHealth {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
opts.WaitIndex = meta.LastIndex
|
catalogOptions.WaitIndex = catalogMeta.LastIndex
|
||||||
|
lastHealthIndex = healthMeta.LastIndex
|
||||||
|
|
||||||
if data != nil {
|
if data != nil {
|
||||||
watchCh <- data
|
watchCh <- data
|
||||||
|
|
Loading…
Reference in a new issue