github.com/cilium/cilium@v1.16.2/pkg/kvstore/heartbeat/heartbeat.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 package heartbeat 5 6 import ( 7 "context" 8 9 "github.com/cilium/cilium/pkg/defaults" 10 "github.com/cilium/cilium/pkg/inctimer" 11 "github.com/cilium/cilium/pkg/kvstore" 12 "github.com/cilium/cilium/pkg/logging" 13 "github.com/cilium/cilium/pkg/logging/logfields" 14 "github.com/cilium/cilium/pkg/time" 15 ) 16 17 var log = logging.DefaultLogger.WithField(logfields.LogSubsys, "kvstore-heartbeat") 18 19 // Heartbeat periodically updates the heatbeat path through the given client, 20 // blocking until the context is canceled. 21 func Heartbeat(ctx context.Context, backend kvstore.BackendOperations) { 22 log.WithField(logfields.Interval, kvstore.HeartbeatWriteInterval).Info("Starting to update heartbeat key") 23 timer, timerDone := inctimer.New() 24 defer timerDone() 25 for { 26 log.Debug("Updating heartbeat key") 27 tctx, cancel := context.WithTimeout(ctx, defaults.LockLeaseTTL) 28 err := backend.Update(tctx, kvstore.HeartbeatPath, []byte(time.Now().Format(time.RFC3339)), true) 29 if err != nil { 30 log.WithError(err).Warning("Unable to update heartbeat key") 31 } 32 cancel() 33 34 select { 35 case <-timer.After(kvstore.HeartbeatWriteInterval): 36 case <-ctx.Done(): 37 log.Info("Stopping to update heartbeat key") 38 return 39 } 40 } 41 }