// Adapted from https://codereview.stackexchange.com/questions/144273/watchdog-in-golang package watchdog import ( "time" ) type watchdog struct { interval time.Duration callback func() timer *time.Timer } func New(interval time.Duration, callback func()) *watchdog { w := watchdog{ interval: interval, callback: callback, timer: nil, } return &w } func (w *watchdog) Start() { w.timer = time.AfterFunc(w.interval, w.callback) } // Helper to allow the watchdog to be started by a call to Kick directly func (w *watchdog) DeferredStart() { w.Start() w.Stop() } func (w *watchdog) Stop() { w.timer.Stop() } func (w *watchdog) Kick() { w.Stop() w.timer.Reset(w.interval) }