Skip to content

Commit

Permalink
Merge pull request #67 from treid314/ring-metrics-loop
Browse files Browse the repository at this point in the history
Move ring metrics go routine and ticker to loop and remove close channel in favor of loop context
  • Loading branch information
Tyler Reid authored Oct 21, 2021
2 parents 3a65fbd + 4bbe2b2 commit 3bd016e
Showing 1 changed file with 6 additions and 18 deletions.
24 changes: 6 additions & 18 deletions ring/ring.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ type Ring struct {
totalTokensGauge prometheus.Gauge
numTokensGaugeVec *prometheus.GaugeVec
oldestTimestampGaugeVec *prometheus.GaugeVec
metricsUpdateCloser chan struct{}

logger log.Logger
}
Expand Down Expand Up @@ -264,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
}

Expand All @@ -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)
Expand All @@ -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")
Expand All @@ -317,14 +313,6 @@ func (r *Ring) loop(ctx context.Context) error {
return nil
}

func (r *Ring) stopping(_ error) error {
// Stop Metrics ticker.
if r.metricsUpdateCloser != nil {
close(r.metricsUpdateCloser)
}
return nil
}

func (r *Ring) updateRingState(ringDesc *Desc) {
r.mtx.RLock()
prevRing := r.ringDesc
Expand Down

0 comments on commit 3bd016e

Please sign in to comment.