Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move ring metrics go routine and ticker to loop and remove close channel in favor of loop context #67

Merged
merged 2 commits into from
Oct 21, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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