evcc-io/core/health.go
2023-12-06 18:29:32 +01:00

42 lines
774 B
Go

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()
}