github.com/msales/pkg/v3@v3.24.0/stats/heartbeat.go (about) 1 package stats 2 3 import ( 4 "context" 5 "time" 6 ) 7 8 // DefaultHeartbeatInterval is the default heartbeat ticker interval. 9 var DefaultHeartbeatInterval = time.Second 10 11 // Heartbeat enters a loop, reporting a heartbeat counter periodically. 12 func Heartbeat(stats Stats) { 13 HeartbeatEvery(stats, DefaultHeartbeatInterval) 14 } 15 16 // HeartbeatEvery enters a loop, reporting a heartbeat counter at the specified interval. 17 func HeartbeatEvery(stats Stats, t time.Duration) { 18 c := time.Tick(t) 19 for range c { 20 _ = stats.Inc("heartbeat", 1, 1.0) 21 } 22 } 23 24 // HeartbeatFromContext is the same as HeartbeatEvery but from context. 25 func HeartbeatFromContext(ctx context.Context, t time.Duration) { 26 if s, ok := FromContext(ctx); ok { 27 HeartbeatEvery(s, t) 28 } 29 }