From 8d1c1676205436048c599cb26017dfd1b942fc65 Mon Sep 17 00:00:00 2001 From: Tyler Reid Date: Wed, 20 Oct 2021 16:45:55 -0500 Subject: [PATCH 1/2] Move ring metrics go routine and ticker to loop and remove close channel in favor of loop context --- ring/ring.go | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/ring/ring.go b/ring/ring.go index 4f60b226f..55eaddb17 100644 --- a/ring/ring.go +++ b/ring/ring.go @@ -198,7 +198,6 @@ type Ring struct { totalTokensGauge prometheus.Gauge numTokensGaugeVec *prometheus.GaugeVec oldestTimestampGaugeVec *prometheus.GaugeVec - metricsUpdateCloser chan struct{} logger log.Logger } @@ -281,11 +280,12 @@ func (r *Ring) starting(ctx context.Context) error { } else { level.Info(r.logger).Log("msg", "ring doesn't exist in KV store yet") } + return nil +} - // Update the ring metrics at start. +func (r *Ring) loop(ctx context.Context) error { + // Update the ring metrics at start of the main loop. r.updateRingMetrics() - // Use this channel to close the go routine to prevent leaks. - r.metricsUpdateCloser = make(chan struct{}) go func() { // Start metrics update ticker to update the ring metrics. ticker := time.NewTicker(10 * time.Second) @@ -295,16 +295,12 @@ func (r *Ring) starting(ctx context.Context) error { select { case <-ticker.C: r.updateRingMetrics() - case <-r.metricsUpdateCloser: + case <-ctx.Done(): return } } }() - return nil -} - -func (r *Ring) loop(ctx context.Context) error { r.KVClient.WatchKey(ctx, r.key, func(value interface{}) bool { if value == nil { level.Info(r.logger).Log("msg", "ring doesn't exist in KV store yet") @@ -318,10 +314,6 @@ func (r *Ring) loop(ctx context.Context) error { } func (r *Ring) stopping(_ error) error { - // Stop Metrics ticker. - if r.metricsUpdateCloser != nil { - close(r.metricsUpdateCloser) - } return nil } From 4bbe2b279599a0af2507518c5e2b3faf7adb894a Mon Sep 17 00:00:00 2001 From: Tyler Reid Date: Thu, 21 Oct 2021 11:55:34 -0500 Subject: [PATCH 2/2] Remove un-used stopping function --- ring/ring.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/ring/ring.go b/ring/ring.go index 55eaddb17..63e3a547c 100644 --- a/ring/ring.go +++ b/ring/ring.go @@ -263,7 +263,7 @@ func NewWithStoreClientAndStrategy(cfg Config, name, key string, store kv.Client logger: logger, } - r.Service = services.NewBasicService(r.starting, r.loop, r.stopping).WithName(fmt.Sprintf("%s ring client", name)) + r.Service = services.NewBasicService(r.starting, r.loop, nil).WithName(fmt.Sprintf("%s ring client", name)) return r, nil } @@ -313,10 +313,6 @@ func (r *Ring) loop(ctx context.Context) error { return nil } -func (r *Ring) stopping(_ error) error { - return nil -} - func (r *Ring) updateRingState(ringDesc *Desc) { r.mtx.RLock() prevRing := r.ringDesc