fix of endless loop while doing Consul service discovery.
Reloading Prometheus configs doesn't make loop end.
It produced a goroutine leak
@@ -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 {
+ 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 {
+ for {
+ select {
+ case <-ctx.Done():
+ ticker.Stop()
+ return
+ default:
srv.watch(ctx, ch, catalog, &lastIndex)
+ <-ticker.C
+ }
}
- <-ctx.Done()
- ticker.Stop()
}()
}
