fix of endless loop while doing Consul service discovery. (#4044)

Reloading Prometheus configs doesn't make loop end.
It produced a goroutine leak
This commit is contained in:
sev3ryn 2018-04-05 11:41:09 +02:00 committed by Brian Brazil
parent 2aba238f31
commit cc917aee7f
1 changed files with 22 additions and 12 deletions

View File

@ -302,17 +302,22 @@ func (d *Discovery) Run(ctx context.Context, ch chan<- []*targetgroup.Group) {
if len(d.watchedServices) == 0 || d.watchedTag != "" {
// We need to watch the catalog.
ticker := time.NewTicker(d.refreshInterval)
go func() {
// Watched services and their cancellation functions.
services := make(map[string]func())
var lastIndex uint64
for ; true; <-ticker.C {
// Watched services and their cancellation functions.
services := make(map[string]func())
var lastIndex uint64
for {
select {
case <-ctx.Done():
ticker.Stop()
return
default:
d.watchServices(ctx, ch, &lastIndex, services)
<-ticker.C
}
}()
<-ctx.Done()
ticker.Stop()
}
} else {
// We only have fully defined services.
for _, name := range d.watchedServices {
@ -417,11 +422,16 @@ func (d *Discovery) watchService(ctx context.Context, ch chan<- []*targetgroup.G
ticker := time.NewTicker(d.refreshInterval)
var lastIndex uint64
catalog := srv.client.Catalog()
for ; true; <-ticker.C {
srv.watch(ctx, ch, catalog, &lastIndex)
for {
select {
case <-ctx.Done():
ticker.Stop()
return
default:
srv.watch(ctx, ch, catalog, &lastIndex)
<-ticker.C
}
}
<-ctx.Done()
ticker.Stop()
}()
}