Heartbeats
Heartbeats are periodic liveness signals. They need clear ownership, interval policy, and shutdown behavior.
Canonical guidance
- use heartbeats as liveness hints, not proofs of health
- stop the ticker when ownership ends
- define what missing a beat should trigger
Use when
- worker liveness reporting
- long-running background loops
- low-cost progress signals
Avoid
- detached heartbeat goroutines
- treating a heartbeat as full service health
- choosing intervals with no relationship to timeout policy
Preferred pattern
- a ticker-driven loop emits a best-effort pulse until
ctx.Done()
Anti-pattern
- a global heartbeat goroutine with no owner and no stop path
Explanation: This is tempting because the signal seems harmless, but periodic work without lifecycle control becomes background noise or leaks.
Why
- liveness signals only help if their semantics and lifecycle are trustworthy
Related pages
Sources
- Go Concurrency Patterns: Context - Sameer Ajmani
- time package - Go Team