lbhealth: ingress-drain condition, listed last
W6 ingress drain spec rev 4 §4.2 step 1. SetIngressDrain(true) fails the health answer with "unavailable: ingress-drain" while every other fact stays as it is. The zero value does not fail it, and a nil state still lists exactly the six earlier conditions.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+52
−1
internal/lbhealth/lbhealth.go+19 −1internal/lbhealth/lbhealth_test.go+33 −0
diff --git a/internal/lbhealth/lbhealth.go b/internal/lbhealth/lbhealth.go| index af3260bf2..eb0284296 100644 |
| --- a/internal/lbhealth/lbhealth.go |
| +++ b/internal/lbhealth/lbhealth.go |
| @@ -43,6 +43,10 @@ const ( |
| CondEdge = "edge" |
| CondActivator = "activator" |
| CondRefresh = "refresh" |
| + // CondIngressDrain is the W6 ingress drain (fixed-edge ingress drain |
| + // spec §4.2 step 1): the box fails its health check on purpose so the |
| + // load balancer stops sending it new flows, while it keeps serving. |
| + CondIngressDrain = "ingress-drain" |
| ) |
| // State is one box's liveness facts. The zero value is a box that has |
| @@ -54,6 +58,7 @@ type State struct { |
| certs atomic.Bool |
| edgeBeat atomic.Int64 // unix nanoseconds; 0 is never |
| refreshed atomic.Int64 // unix nanoseconds; 0 is never |
| + draining atomic.Bool // the ingress drain is on; false in the zero value |
| mu sync.RWMutex |
| activator func() time.Time |
| @@ -114,6 +119,16 @@ func (s *State) RefreshAttempted(t time.Time) { |
| } |
| } |
| +// SetIngressDrain turns the ingress drain's condition on or off. While it |
| +// is on the health check answers 503 and nothing else about the box |
| +// changes: the drain wants the load balancer to withdraw the box, not the |
| +// box to stop serving. |
| +func (s *State) SetIngressDrain(on bool) { |
| + if s != nil { |
| + s.draining.Store(on) |
| + } |
| +} |
| + |
| // SetActivator gives the state the activator's own beat: the time its |
| // sweeper last finished a sweep. nil means there is no activator, which |
| // fails the activator condition. |
| @@ -150,7 +165,7 @@ func stamp(n int64) time.Time { |
| // Failing lists the conditions that do not hold at now, in the order of |
| // the Cond names. An empty list is a healthy box. A nil *State fails |
| -// every condition. |
| +// every condition except the ingress drain, which only a drain turns on. |
| func (s *State) Failing(now time.Time) []string { |
| if s == nil { |
| return []string{CondHosting, CondForwarding, CondCerts, CondEdge, CondActivator, CondRefresh} |
| @@ -174,6 +189,9 @@ func (s *State) Failing(now time.Time) []string { |
| if !within(stamp(s.refreshed.Load()), now, RefreshWindow) { |
| out = append(out, CondRefresh) |
| } |
| + if s.draining.Load() { |
| + out = append(out, CondIngressDrain) |
| + } |
| return out |
| } |
diff --git a/internal/lbhealth/lbhealth_test.go b/internal/lbhealth/lbhealth_test.go| index 9bf211ec9..5b5f35f7e 100644 |
| --- a/internal/lbhealth/lbhealth_test.go |
| +++ b/internal/lbhealth/lbhealth_test.go |
| @@ -131,3 +131,36 @@ func TestNilStateIgnoresUpdates(t *testing.T) { |
| t.Fatal("nil state hosting") |
| } |
| } |
| + |
| +// The ingress drain (W6 §4.2 step 1) fails the answer on its own, is |
| +// listed after the six earlier conditions, and is off in the zero value. |
| +// A nil state still lists exactly the six: a nil state is not draining. |
| +func TestIngressDrainCondition(t *testing.T) { |
| + s := healthy() |
| + s.SetIngressDrain(true) |
| + if got := s.Failing(t0); !reflect.DeepEqual(got, []string{CondIngressDrain}) { |
| + t.Fatalf("draining healthy box fails %v", got) |
| + } |
| + w := httptest.NewRecorder() |
| + Serve(w, httptest.NewRequest(http.MethodGet, Path, nil), s, t0) |
| + if w.Code != http.StatusServiceUnavailable || w.Body.String() != "unavailable: ingress-drain\n" { |
| + t.Fatalf("draining GET: %d %q", w.Code, w.Body.String()) |
| + } |
| + s.SetIngressDrain(false) |
| + if got := s.Failing(t0); len(got) != 0 { |
| + t.Fatalf("drain cleared, still fails %v", got) |
| + } |
| + |
| + z := &State{} |
| + z.SetIngressDrain(true) |
| + want := []string{CondHosting, CondForwarding, CondCerts, CondEdge, CondActivator, CondRefresh, CondIngressDrain} |
| + if got := z.Failing(t0); !reflect.DeepEqual(got, want) { |
| + t.Fatalf("zero state draining fails %v, want %v", got, want) |
| + } |
| + |
| + var nilState *State |
| + nilState.SetIngressDrain(true) |
| + if got := nilState.Failing(t0); !reflect.DeepEqual(got, want[:6]) { |
| + t.Fatalf("nil state fails %v", got) |
| + } |
| +} |