forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhealth.go
42 lines (33 loc) · 774 Bytes
/
health.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package core
import (
"sync"
"time"
)
// Health is a health checker that needs regular updates to stay healthy
type Health struct {
mux sync.Mutex
updated time.Time
timeout time.Duration
}
// NewHealth creates new health checker
func NewHealth(timeout time.Duration) *Health {
return &Health{timeout: timeout}
}
// Healthy returns health status based on last update timestamp
func (health *Health) Healthy() bool {
if health == nil {
return false
}
health.mux.Lock()
defer health.mux.Unlock()
return time.Since(health.updated) < health.timeout
}
// Update updates the health timer on each loadpoint update
func (health *Health) Update() {
if health == nil {
return
}
health.mux.Lock()
defer health.mux.Unlock()
health.updated = time.Now()
}