Tickers
Stop tickers you create, prefer timers for one-shot waits, and do not let periodic work outlive its owner.
Canonical guidance
- use tickers for repeated work, timers for one-shot waits
- stop tickers you create when the owner is done
- make periodic work cancellable
Use when
- polling loops
- periodic flush or cleanup work
- background heartbeats
Avoid
- using tickers for single delayed events
- forgetting to stop a ticker in bounded workflows
- assuming ticker cadence stays exact under load
Preferred pattern
ticker := time.NewTicker(30 * time.Second)
defer ticker.Stop()
Anti-pattern
- starting a ticker in a goroutine with no cancellation path and no clear owner
Explanation: This anti-pattern is tempting because periodic work looks harmless, but it quietly creates runaway background loops.
Why
- ticker lifecycle is resource management, not just syntax
Related pages
Sources
- time package - Go Team