kubeprobes/stateful_probe.go

53 lines
1.1 KiB
Go
Raw Normal View History

2023-07-21 23:12:18 +02:00
package kubeprobes
import (
"errors"
"sync"
)
var errProbeDown = errors.New("probe is down")
2023-07-21 23:12:18 +02:00
// StatefulProbe represents the simple probe that can be either
// marked as "up" (healthy) or "down" (unhealthy).
type StatefulProbe struct {
mux sync.Mutex
status bool
2023-07-21 23:12:18 +02:00
}
// NewStatefulProbe returns a new instance of a stateful probe
// which can be either marked as "up" (healthy) or "down" (unhealthy).
// The probe is initially marked as "down".
func NewStatefulProbe() *StatefulProbe {
return &StatefulProbe{
mux: sync.Mutex{},
status: false,
2023-07-21 23:12:18 +02:00
}
}
// MarkAsUp marks the probe as healthy.
2023-07-21 23:12:18 +02:00
func (sp *StatefulProbe) MarkAsUp() {
sp.mux.Lock()
defer sp.mux.Unlock()
sp.status = true
}
// MarkAsDown marks the probe as unhealthy.
2023-07-21 23:12:18 +02:00
func (sp *StatefulProbe) MarkAsDown() {
sp.mux.Lock()
defer sp.mux.Unlock()
sp.status = false
}
// GetProbeFunction returns a function that can be used to check
// whether the probe is healthy or not.
func (sp *StatefulProbe) GetProbeFunction() ProbeFunction {
return func() error {
sp.mux.Lock()
defer sp.mux.Unlock()
if sp.status {
return nil
}
return errProbeDown
}
}