github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/swarmkit/manager/dispatcher/heartbeat/heartbeat.go (about) 1 package heartbeat 2 3 import ( 4 "sync/atomic" 5 "time" 6 ) 7 8 // Heartbeat is simple way to track heartbeats. 9 type Heartbeat struct { 10 timeout int64 11 timer *time.Timer 12 } 13 14 // New creates new Heartbeat with specified duration. timeoutFunc will be called 15 // if timeout for heartbeat is expired. Note that in case of timeout you need to 16 // call Beat() to reactivate Heartbeat. 17 func New(timeout time.Duration, timeoutFunc func()) *Heartbeat { 18 hb := &Heartbeat{ 19 timeout: int64(timeout), 20 timer: time.AfterFunc(timeout, timeoutFunc), 21 } 22 return hb 23 } 24 25 // Beat resets internal timer to zero. It also can be used to reactivate 26 // Heartbeat after timeout. 27 func (hb *Heartbeat) Beat() { 28 hb.timer.Reset(time.Duration(atomic.LoadInt64(&hb.timeout))) 29 } 30 31 // Update updates internal timeout to d. It does not do Beat. 32 func (hb *Heartbeat) Update(d time.Duration) { 33 atomic.StoreInt64(&hb.timeout, int64(d)) 34 } 35 36 // Stop stops Heartbeat timer. 37 func (hb *Heartbeat) Stop() { 38 hb.timer.Stop() 39 }