Some output tweaks; and some additional transition events upon resume (paused->unknown->{up|down})
This commit is contained in:
@@ -35,6 +35,7 @@ type worker struct {
|
||||
hc config.HealthCheck
|
||||
entry config.Backend
|
||||
cancel context.CancelFunc
|
||||
wakeCh chan struct{} // closed/signalled to interrupt probe sleep on resume
|
||||
}
|
||||
|
||||
// Checker orchestrates health probing for all backends.
|
||||
@@ -236,8 +237,12 @@ func (c *Checker) PauseBackend(name string) (BackendSnapshot, bool) {
|
||||
}
|
||||
maxHistory := c.cfg.HealthChecker.TransitionHistory
|
||||
if w.backend.Pause(maxHistory) {
|
||||
slog.Info("backend-pause", "backend", name)
|
||||
c.emitForBackend(name, w.backend.Address, w.backend.Transitions[0], c.cfg.Frontends)
|
||||
t := w.backend.Transitions[0]
|
||||
slog.Info("backend-transition", "backend", name,
|
||||
"from", t.From.String(),
|
||||
"to", t.To.String(),
|
||||
)
|
||||
c.emitForBackend(name, w.backend.Address, t, c.cfg.Frontends)
|
||||
}
|
||||
return BackendSnapshot{Health: w.backend, Config: w.entry}, true
|
||||
}
|
||||
@@ -252,8 +257,16 @@ func (c *Checker) ResumeBackend(name string) (BackendSnapshot, bool) {
|
||||
}
|
||||
maxHistory := c.cfg.HealthChecker.TransitionHistory
|
||||
if w.backend.Resume(maxHistory) {
|
||||
slog.Info("backend-resume", "backend", name)
|
||||
c.emitForBackend(name, w.backend.Address, w.backend.Transitions[0], c.cfg.Frontends)
|
||||
t := w.backend.Transitions[0]
|
||||
slog.Info("backend-transition", "backend", name,
|
||||
"from", t.From.String(),
|
||||
"to", t.To.String(),
|
||||
)
|
||||
c.emitForBackend(name, w.backend.Address, t, c.cfg.Frontends)
|
||||
select {
|
||||
case w.wakeCh <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
}
|
||||
return BackendSnapshot{Health: w.backend, Config: w.entry}, true
|
||||
}
|
||||
@@ -274,6 +287,7 @@ func (c *Checker) startWorker(ctx context.Context, name string, entry config.Bac
|
||||
hc: hc,
|
||||
entry: entry,
|
||||
cancel: cancel,
|
||||
wakeCh: make(chan struct{}, 1),
|
||||
}
|
||||
w.backend.Start(maxHistory)
|
||||
c.workers[name] = w
|
||||
@@ -310,6 +324,7 @@ func (c *Checker) runProbe(ctx context.Context, name string, pos, total int) {
|
||||
entry := w.entry
|
||||
maxHistory := c.cfg.HealthChecker.TransitionHistory
|
||||
netns := c.cfg.HealthChecker.Netns
|
||||
wakeCh := w.wakeCh
|
||||
var sleepFor time.Duration
|
||||
if entry.HealthCheck == "" {
|
||||
sleepFor = 30 * time.Second
|
||||
@@ -322,6 +337,7 @@ func (c *Checker) runProbe(ctx context.Context, name string, pos, total int) {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-time.After(sleepFor):
|
||||
case <-wakeCh:
|
||||
}
|
||||
|
||||
var result health.ProbeResult
|
||||
|
||||
@@ -104,13 +104,15 @@ type Backend struct {
|
||||
Transitions []Transition // newest first, capped at maxHistory
|
||||
}
|
||||
|
||||
// New creates a Backend in StateUnknown.
|
||||
// New creates a Backend in StateUnknown with the health counter pre-loaded to
|
||||
// Rise-1, so the very first probe resolves the state: one pass → Up, any
|
||||
// fail → Down (via the StateUnknown shortcut in Record).
|
||||
func New(name string, addr net.IP, rise, fall int) *Backend {
|
||||
return &Backend{
|
||||
Name: name,
|
||||
Address: addr,
|
||||
State: StateUnknown,
|
||||
Counter: HealthCounter{Rise: rise, Fall: fall},
|
||||
Counter: HealthCounter{Rise: rise, Fall: fall, Health: rise - 1},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -155,7 +157,7 @@ func (b *Backend) Resume(maxHistory int) bool {
|
||||
return false
|
||||
}
|
||||
b.transition(StateUnknown, ProbeResult{}, maxHistory)
|
||||
b.Counter.Health = 0
|
||||
b.Counter.Health = b.Counter.Rise - 1
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
@@ -23,12 +23,14 @@ func TestInitialState(t *testing.T) {
|
||||
if len(b.Transitions) != 0 {
|
||||
t.Errorf("initial transitions: got %d, want 0", len(b.Transitions))
|
||||
}
|
||||
if b.Counter.Health != 0 {
|
||||
t.Errorf("initial counter health: got %d, want 0", b.Counter.Health)
|
||||
// Counter pre-loaded to Rise-1 so first probe resolves state immediately.
|
||||
if want := b.Counter.Rise - 1; b.Counter.Health != want {
|
||||
t.Errorf("initial counter health: got %d, want %d (rise-1)", b.Counter.Health, want)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRiseToUp: rise=2 passes from Down/Unknown → Up.
|
||||
// TestRiseToUp: from Unknown/Down with counter pre-loaded to Rise-1, a single
|
||||
// pass is enough to reach Up.
|
||||
func TestRiseToUp(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -39,18 +41,11 @@ func TestRiseToUp(t *testing.T) {
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
b := newBackend()
|
||||
b := newBackend() // rise=2 → Health starts at Rise-1=1
|
||||
b.State = tt.initialState
|
||||
// First pass: counter=1, still in DOWN range (rise=2), no transition.
|
||||
if b.Record(pass(), 5) {
|
||||
t.Error("should not transition after 1 pass (rise=2)")
|
||||
}
|
||||
if b.State != tt.initialState {
|
||||
t.Errorf("state changed early: got %s", b.State)
|
||||
}
|
||||
// Second pass: counter=2=rise, transitions to Up.
|
||||
// Counter is already at Rise-1; one pass reaches Rise → Up.
|
||||
if !b.Record(pass(), 5) {
|
||||
t.Error("should transition to Up after 2 passes")
|
||||
t.Error("should transition to Up after 1 pass (counter pre-loaded to rise-1)")
|
||||
}
|
||||
if b.State != StateUp {
|
||||
t.Errorf("state: got %s, want up", b.State)
|
||||
|
||||
Reference in New Issue
Block a user