kubeprobes/probe_manual.go
Piotr Icikowski 8dc7f27400
All checks were successful
ci/woodpecker/push/release Pipeline was successful
ci/woodpecker/push/test Pipeline was successful
refactor(probes): rename ProbeFunction to Probe
2024-03-03 23:25:56 +01:00

72 lines
1.4 KiB
Go

package kubeprobes
import (
"sync"
)
// ManualProbe represents the simple probe that can be either
// marked as "up" (healthy) or "down" (unhealthy).
type ManualProbe interface {
Probe
// Pass marks the probe as healthy.
Pass()
// Fail marks the probe as unhealthy.
Fail()
// FailWitCause marks the probe as unhealthy with given cause.
FailWithCause(err error)
}
type manualProbe struct {
probeName string
err error
mux sync.RWMutex
}
// NewManualProbe returns a new instance of a manual probe
// which can be either marked as healthy or unhealthy.
// The probe is initially marked as unhealthy.
func NewManualProbe(name string) (ManualProbe, error) {
if name == "" {
return nil, errProbeNameEmpty
}
return &manualProbe{
probeName: name,
mux: sync.RWMutex{},
}, nil
}
// name implements ManualProbe.
func (mp *manualProbe) name() string {
return mp.probeName
}
// status implements ManualProbe.
func (mp *manualProbe) status() error {
mp.mux.RLock()
defer mp.mux.RUnlock()
return mp.err
}
// Pass implements ManualProbe.
func (mp *manualProbe) Pass() {
mp.mux.Lock()
defer mp.mux.Unlock()
mp.err = nil
}
// Fail implements ManualProbe.
func (mp *manualProbe) Fail() {
mp.mux.Lock()
defer mp.mux.Unlock()
mp.err = errProbeFailed
}
// FailWithCause implements ManualProbe.
func (mp *manualProbe) FailWithCause(err error) {
mp.mux.Lock()
defer mp.mux.Unlock()
mp.err = err
}