2018-10-19 16:27:05 +02:00
|
|
|
package watchdog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2018-10-21 12:53:34 +02:00
|
|
|
type KeepAlive struct {
|
|
|
|
mtx sync.Mutex
|
2018-10-19 16:27:05 +02:00
|
|
|
lastUpd time.Time
|
|
|
|
}
|
|
|
|
|
2018-10-21 12:53:34 +02:00
|
|
|
func (p *KeepAlive) String() string {
|
|
|
|
if p.lastUpd.IsZero() {
|
|
|
|
return fmt.Sprintf("never updated")
|
2018-10-19 16:27:05 +02:00
|
|
|
}
|
2018-10-21 12:53:34 +02:00
|
|
|
return fmt.Sprintf("last update at %s", p.lastUpd)
|
2018-10-19 16:27:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (k *KeepAlive) MadeProgress() {
|
|
|
|
k.mtx.Lock()
|
|
|
|
defer k.mtx.Unlock()
|
2018-10-21 12:53:34 +02:00
|
|
|
k.lastUpd = time.Now()
|
2018-10-19 16:27:05 +02:00
|
|
|
}
|
|
|
|
|
2018-10-21 12:53:34 +02:00
|
|
|
func (k *KeepAlive) CheckTimeout(timeout time.Duration, jitter time.Duration) (didTimeOut bool) {
|
2018-10-19 16:27:05 +02:00
|
|
|
k.mtx.Lock()
|
|
|
|
defer k.mtx.Unlock()
|
2018-10-21 12:53:34 +02:00
|
|
|
return k.lastUpd.Add(timeout - jitter).Before(time.Now())
|
2018-10-19 16:27:05 +02:00
|
|
|
}
|