kubeprobes/probe_manual.go

72 lines
1.4 KiB
Go
Raw Normal View History

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
}