Skip to content

Commit b4c37ce

Browse files
authored
Add timeout on lifecycler heartbeat (#6212)
* Add timeout on http requests for kv store DynamoDB client Signed-off-by: Anna Tran <[email protected]> * Defer cancel on timeout for heartbeat Signed-off-by: Anna Tran <[email protected]> --------- Signed-off-by: Anna Tran <[email protected]>
1 parent 582c03a commit b4c37ce

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

pkg/ring/lifecycler.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ func (i *Lifecycler) loop(ctx context.Context) error {
468468
// We are jittering for at least half of the time and max the time of the heartbeat.
469469
// If we jitter too soon, we can have problems of concurrency with autoJoin leaving the instance on ACTIVE without tokens
470470
time.AfterFunc(time.Duration(uint64(i.cfg.HeartbeatPeriod/2)+uint64(mathrand.Int63())%uint64(i.cfg.HeartbeatPeriod/2)), func() {
471-
i.heartbeat()
471+
i.heartbeat(ctx)
472472
heartbeatTicker.Reset(i.cfg.HeartbeatPeriod)
473473
})
474474
defer heartbeatTicker.Stop()
@@ -530,7 +530,7 @@ func (i *Lifecycler) loop(ctx context.Context) error {
530530
}
531531

532532
case <-heartbeatTickerChan:
533-
i.heartbeat()
533+
i.heartbeat(ctx)
534534
case f := <-i.actorChan:
535535
f()
536536

@@ -541,9 +541,11 @@ func (i *Lifecycler) loop(ctx context.Context) error {
541541
}
542542
}
543543

544-
func (i *Lifecycler) heartbeat() {
544+
func (i *Lifecycler) heartbeat(ctx context.Context) {
545545
i.lifecyclerMetrics.consulHeartbeats.Inc()
546-
if err := i.updateConsul(context.Background()); err != nil {
546+
ctx, cancel := context.WithTimeout(ctx, i.cfg.HeartbeatPeriod)
547+
defer cancel()
548+
if err := i.updateConsul(ctx); err != nil {
547549
level.Error(i.logger).Log("msg", "failed to write to the KV store, sleeping", "ring", i.RingName, "err", err)
548550
}
549551
}

0 commit comments

Comments
 (0)