kubeprobes/stateful_probe.go
Piotr Icikowski d33e9f19ea
refactor(kubeprobes): refactor code
- refactored types, interfaces, options etc.
- added new options dedicated for `StatefulProbe`s
2024-03-01 23:32:11 +01:00

53 lines
1.1 KiB
Go

package kubeprobes
import (
"errors"
"sync"
)
var errProbeDown = errors.New("probe is down")
// StatefulProbe represents the simple probe that can be either
// marked as "up" (healthy) or "down" (unhealthy).
type StatefulProbe struct {
mux sync.Mutex
status bool
}
// 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,
}
}
// MarkAsUp marks the probe as healthy.
func (sp *StatefulProbe) MarkAsUp() {
sp.mux.Lock()
defer sp.mux.Unlock()
sp.status = true
}
// MarkAsDown marks the probe as unhealthy.
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
}
}