edge: held-leg methods on the Activator seam; countingWriter unwraps (#432)
Human
Prompt
Task 3 of #432, edge seam and writer plumbing.
From this commit's own message. The prompt ledger has no entries for it — which is what a squashed, rebased or amended commit looks like, and what every commit looks like in a repo without a ledger.
Summary
Activator gains AdmitHeldLeg, HeldAdmissible and SetHeldWakeHook. countingWriter.Unwrap lets http.NewResponseController reach the real writer. Test fakes updated; internal/host asserts activator.Service satisfies edge.Activator.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
13 files changed,
+122
−0
internal/domains/rootfs_apex_test.go+6 −0internal/edge/alias_test.go+7 −0internal/edge/edge.go+8 −0internal/edge/edge_test.go+20 −0internal/edge/edgeerrors_test.go+10 −0internal/edge/forwardha_test.go+10 −0internal/edge/genedge_test.go+7 −0internal/edge/guard_test.go+6 −0internal/edge/ratelimit_test.go+6 −0internal/edge/reqrecords_test.go+7 −0internal/edge/usagemeter.go+4 −0internal/edge/usagemeter_test.go+21 −0internal/host/activator_seam.go+10 −0
diff --git a/internal/domains/rootfs_apex_test.go b/internal/domains/rootfs_apex_test.go| index f3704e047..c47e7e7e1 100644 |
| --- a/internal/domains/rootfs_apex_test.go |
| +++ b/internal/domains/rootfs_apex_test.go |
| @@ -1,6 +1,7 @@ |
| package domains_test |
| import ( |
| + "context" |
| "errors" |
| "io" |
| "net" |
| @@ -28,6 +29,11 @@ func (a *apexActivator) ServeHTTP(w http.ResponseWriter, r *http.Request, rt reg |
| } |
| func (a *apexActivator) RetireExcept(map[string]bool) {} |
| +func (a *apexActivator) AdmitHeldLeg(context.Context, registry.Route) (edge.HeldLeg, error) { |
| + return nil, errors.New("asleep") |
| +} |
| +func (a *apexActivator) HeldAdmissible(string) bool { return false } |
| +func (a *apexActivator) SetHeldWakeHook(func(string)) {} |
| // TestPlatformApexOfRootfsIsAnAlias: the platform apex minted from a |
| // resident rootfs instance copies its upstream and nothing of its runtime, |
diff --git a/internal/edge/alias_test.go b/internal/edge/alias_test.go| index 2627c867b..51bd250e3 100644 |
| --- a/internal/edge/alias_test.go |
| +++ b/internal/edge/alias_test.go |
| @@ -1,6 +1,8 @@ |
| package edge |
| import ( |
| + "context" |
| + "errors" |
| "maps" |
| "net/http" |
| "net/http/httptest" |
| @@ -283,6 +285,11 @@ func (a *routeRecordingActivator) ServeHTTP(w http.ResponseWriter, r *http.Reque |
| } |
| func (a *routeRecordingActivator) RetireExcept(keep map[string]bool) {} |
| +func (a *routeRecordingActivator) AdmitHeldLeg(context.Context, registry.Route) (HeldLeg, error) { |
| + return nil, errors.New("asleep") |
| +} |
| +func (a *routeRecordingActivator) HeldAdmissible(string) bool { return false } |
| +func (a *routeRecordingActivator) SetHeldWakeHook(func(string)) {} |
| // TestAliasWakesPrimaryOnce is the #129-class regression pin: a request to an |
| // attached hostname wakes the PRIMARY's single activator entry. The activator |
diff --git a/internal/edge/edge.go b/internal/edge/edge.go| index d0beb2979..0f5cd5c57 100644 |
| --- a/internal/edge/edge.go |
| +++ b/internal/edge/edge.go |
| @@ -93,6 +93,14 @@ const DefaultCacheMaxTTL = 5 * time.Minute |
| type Activator interface { |
| ServeHTTP(w http.ResponseWriter, r *http.Request, rt registry.Route, proxy http.Handler) bool |
| RetireExcept(keep map[string]bool) |
| + // AdmitHeldLeg admits one upstream leg of an edge-held event stream, or |
| + // errors when the instance is not awake on a current generation. |
| + AdmitHeldLeg(ctx context.Context, rt registry.Route) (HeldLeg, error) |
| + // HeldAdmissible reports whether host is hibernate-managed here, so a |
| + // stream request may be held. |
| + HeldAdmissible(host string) bool |
| + // SetHeldWakeHook installs the callback that wakes host for a held stream. |
| + SetHeldWakeHook(f func(host string)) |
| } |
| // Edge terminates TLS (autocert) and reverse-proxies each configured host to |
diff --git a/internal/edge/edge_test.go b/internal/edge/edge_test.go| index 905802114..1dcf1c912 100644 |
| --- a/internal/edge/edge_test.go |
| +++ b/internal/edge/edge_test.go |
| @@ -263,6 +263,11 @@ func (f *fakeActivator) ServeHTTP(w http.ResponseWriter, r *http.Request, rt reg |
| func (f *fakeActivator) RetireExcept(keep map[string]bool) { |
| f.retired = append(f.retired, keep) |
| } |
| +func (f *fakeActivator) AdmitHeldLeg(context.Context, registry.Route) (HeldLeg, error) { |
| + return nil, errors.New("asleep") |
| +} |
| +func (f *fakeActivator) HeldAdmissible(string) bool { return false } |
| +func (f *fakeActivator) SetHeldWakeHook(func(string)) {} |
| // testEdgeWithStore builds an Edge with one route already loaded and static |
| // serving (the plumbing kind=status shares — see EnableStatic's doc comment) |
| @@ -490,6 +495,11 @@ func (r refusingActivator) ServeHTTP(w http.ResponseWriter, req *http.Request, r |
| } |
| func (r refusingActivator) RetireExcept(keep map[string]bool) {} |
| +func (r refusingActivator) AdmitHeldLeg(context.Context, registry.Route) (HeldLeg, error) { |
| + return nil, errors.New("asleep") |
| +} |
| +func (r refusingActivator) HeldAdmissible(string) bool { return false } |
| +func (r refusingActivator) SetHeldWakeHook(func(string)) {} |
| func TestServeHTTPCacheHitNeverInvokesActivator(t *testing.T) { |
| reg := openReg(t) |
| @@ -1291,6 +1301,11 @@ func (h hijackCheckingActivator) ServeHTTP(w http.ResponseWriter, r *http.Reques |
| } |
| func (h hijackCheckingActivator) RetireExcept(keep map[string]bool) {} |
| +func (h hijackCheckingActivator) AdmitHeldLeg(context.Context, registry.Route) (HeldLeg, error) { |
| + return nil, errors.New("asleep") |
| +} |
| +func (h hijackCheckingActivator) HeldAdmissible(string) bool { return false } |
| +func (h hijackCheckingActivator) SetHeldWakeHook(func(string)) {} |
| // TestServeHTTPUpgradeRequestBypassesRecording proves a Connection: Upgrade |
| // request (a WebSocket handshake) to a hibernating route reaches the |
| @@ -1350,6 +1365,11 @@ func (f activatorFunc) ServeHTTP(w http.ResponseWriter, r *http.Request, rt regi |
| func (f activatorFunc) RetireExcept(keep map[string]bool) { |
| // No-op for tests that don't need retirement tracking. |
| } |
| +func (f activatorFunc) AdmitHeldLeg(context.Context, registry.Route) (HeldLeg, error) { |
| + return nil, errors.New("asleep") |
| +} |
| +func (f activatorFunc) HeldAdmissible(string) bool { return false } |
| +func (f activatorFunc) SetHeldWakeHook(func(string)) {} |
| // TestCacheBeforeWakeFullCycleOverPurgeSocket is the full cycle with the REAL |
| // purge channel in it: the same cold-miss → cached-repeat → purge → fresh |
diff --git a/internal/edge/edgeerrors_test.go b/internal/edge/edgeerrors_test.go| index 59a51b261..c8ea9a4e4 100644 |
| --- a/internal/edge/edgeerrors_test.go |
| +++ b/internal/edge/edgeerrors_test.go |
| @@ -112,6 +112,11 @@ func (answering503) ServeHTTP(w http.ResponseWriter, r *http.Request, rt registr |
| return true |
| } |
| func (answering503) RetireExcept(map[string]bool) {} |
| +func (answering503) AdmitHeldLeg(context.Context, registry.Route) (HeldLeg, error) { |
| + return nil, errors.New("asleep") |
| +} |
| +func (answering503) HeldAdmissible(string) bool { return false } |
| +func (answering503) SetHeldWakeHook(func(string)) {} |
| func TestEdgeSkipsActivatorAnswered503(t *testing.T) { |
| reg := openReg(t) |
| @@ -146,6 +151,11 @@ func (plain503) ServeHTTP(w http.ResponseWriter, r *http.Request, rt registry.Ro |
| return true |
| } |
| func (plain503) RetireExcept(map[string]bool) {} |
| +func (plain503) AdmitHeldLeg(context.Context, registry.Route) (HeldLeg, error) { |
| + return nil, errors.New("asleep") |
| +} |
| +func (plain503) HeldAdmissible(string) bool { return false } |
| +func (plain503) SetHeldWakeHook(func(string)) {} |
| // TestEdgeEmitsUnmarked503OnHibernatingRoute is the positive control for |
| // TestEdgeSkipsActivatorAnswered503: with the MarkAnswered call gone, the |
diff --git a/internal/edge/forwardha_test.go b/internal/edge/forwardha_test.go| index c33896b86..124033727 100644 |
| --- a/internal/edge/forwardha_test.go |
| +++ b/internal/edge/forwardha_test.go |
| @@ -94,6 +94,11 @@ func (a *fwAct) ServeHTTP(w http.ResponseWriter, r *http.Request, _ registry.Rou |
| return false |
| } |
| func (a *fwAct) RetireExcept(map[string]bool) {} |
| +func (a *fwAct) AdmitHeldLeg(context.Context, registry.Route) (HeldLeg, error) { |
| + return nil, errors.New("asleep") |
| +} |
| +func (a *fwAct) HeldAdmissible(string) bool { return false } |
| +func (a *fwAct) SetHeldWakeHook(func(string)) {} |
| func (a *fwAct) ForwardTarget(_ context.Context, _ registry.Route, _ func() boxchoice.WakeSnapshot) ForwardDecision { |
| a.asked.Add(1) |
| return ForwardDecision{Ref: a.target, Refuse: a.refuse, Gen: a.gen, Kind: a.kind, Unavailable: a.unavailable} |
| @@ -118,6 +123,11 @@ func (fwPlainAct) ServeHTTP(http.ResponseWriter, *http.Request, registry.Route, |
| return false |
| } |
| func (fwPlainAct) RetireExcept(map[string]bool) {} |
| +func (fwPlainAct) AdmitHeldLeg(context.Context, registry.Route) (HeldLeg, error) { |
| + return nil, errors.New("asleep") |
| +} |
| +func (fwPlainAct) HeldAdmissible(string) bool { return false } |
| +func (fwPlainAct) SetHeldWakeHook(func(string)) {} |
| // fwApp is one box's app behind every forward host. Its ordinary body names |
| // the box, the host and the path; /missing is a 404; /holder answers 503 with |
diff --git a/internal/edge/genedge_test.go b/internal/edge/genedge_test.go| index 4f28d8b70..a37ebbe53 100644 |
| --- a/internal/edge/genedge_test.go |
| +++ b/internal/edge/genedge_test.go |
| @@ -1,6 +1,8 @@ |
| package edge |
| import ( |
| + "context" |
| + "errors" |
| "net/http" |
| "path/filepath" |
| "sync/atomic" |
| @@ -25,6 +27,11 @@ func (a *genActivator) ServeHTTP(w http.ResponseWriter, r *http.Request, _ regis |
| } |
| func (a *genActivator) RetireExcept(map[string]bool) {} |
| +func (a *genActivator) AdmitHeldLeg(context.Context, registry.Route) (HeldLeg, error) { |
| + return nil, errors.New("asleep") |
| +} |
| +func (a *genActivator) HeldAdmissible(string) bool { return false } |
| +func (a *genActivator) SetHeldWakeHook(func(string)) {} |
| // genFixture is a registry with one activator-managed instance whose |
| // CANONICAL socket is served by an app answering "canonical", and a |
diff --git a/internal/edge/guard_test.go b/internal/edge/guard_test.go| index 4888b5c2c..85e41441c 100644 |
| --- a/internal/edge/guard_test.go |
| +++ b/internal/edge/guard_test.go |
| @@ -2,6 +2,7 @@ package edge |
| import ( |
| "context" |
| + "errors" |
| "io" |
| "net/http" |
| "net/http/httptest" |
| @@ -267,6 +268,11 @@ func (a *outcomeActivator) ServeHTTP(w http.ResponseWriter, r *http.Request, rt |
| return true |
| } |
| func (a *outcomeActivator) RetireExcept(map[string]bool) {} |
| +func (a *outcomeActivator) AdmitHeldLeg(context.Context, registry.Route) (HeldLeg, error) { |
| + return nil, errors.New("asleep") |
| +} |
| +func (a *outcomeActivator) HeldAdmissible(string) bool { return false } |
| +func (a *outcomeActivator) SetHeldWakeHook(func(string)) {} |
| // An ordinary request matches no rule: it is filed as a plain response and |
| // the guard leaves no verdict on it. |
diff --git a/internal/edge/ratelimit_test.go b/internal/edge/ratelimit_test.go| index 1cc774234..3a4b28358 100644 |
| --- a/internal/edge/ratelimit_test.go |
| +++ b/internal/edge/ratelimit_test.go |
| @@ -7,6 +7,7 @@ package edge |
| import ( |
| "bufio" |
| "context" |
| + "errors" |
| "fmt" |
| "io" |
| "net" |
| @@ -279,6 +280,11 @@ func (a *sleepyActivator) ServeHTTP(w http.ResponseWriter, r *http.Request, rt r |
| } |
| func (a *sleepyActivator) RetireExcept(map[string]bool) {} |
| +func (a *sleepyActivator) AdmitHeldLeg(context.Context, registry.Route) (HeldLeg, error) { |
| + return nil, errors.New("asleep") |
| +} |
| +func (a *sleepyActivator) HeldAdmissible(string) bool { return false } |
| +func (a *sleepyActivator) SetHeldWakeHook(func(string)) {} |
| func (a *sleepyActivator) wakeCount() int { |
| a.mu.Lock() |
diff --git a/internal/edge/reqrecords_test.go b/internal/edge/reqrecords_test.go| index a0f136d42..f1effb46e 100644 |
| --- a/internal/edge/reqrecords_test.go |
| +++ b/internal/edge/reqrecords_test.go |
| @@ -1,6 +1,8 @@ |
| package edge |
| import ( |
| + "context" |
| + "errors" |
| "net/http" |
| "net/http/httptest" |
| "testing" |
| @@ -116,6 +118,11 @@ func (wakingActivator) ServeHTTP(w http.ResponseWriter, r *http.Request, rt regi |
| return true |
| } |
| func (wakingActivator) RetireExcept(map[string]bool) {} |
| +func (wakingActivator) AdmitHeldLeg(context.Context, registry.Route) (HeldLeg, error) { |
| + return nil, errors.New("asleep") |
| +} |
| +func (wakingActivator) HeldAdmissible(string) bool { return false } |
| +func (wakingActivator) SetHeldWakeHook(func(string)) {} |
| // What the activator marks on the request reaches that request's record. |
| func TestRecordsCarryTheWake(t *testing.T) { |
diff --git a/internal/edge/usagemeter.go b/internal/edge/usagemeter.go| index 932b8106c..fced0b8fb 100644 |
| --- a/internal/edge/usagemeter.go |
| +++ b/internal/edge/usagemeter.go |
| @@ -191,6 +191,10 @@ func (c *countingWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) { |
| return hj.Hijack() |
| } |
| +// Unwrap lets http.NewResponseController reach the underlying writer (write |
| +// deadlines for held event streams). |
| +func (c *countingWriter) Unwrap() http.ResponseWriter { return c.ResponseWriter } |
| + |
| // Flush forwards to the underlying ResponseWriter's http.Flusher, when it |
| // implements one — needed so a metered (non-upgrade) response can still |
| // stream incrementally to the real client, e.g. Server-Sent Events, instead |
diff --git a/internal/edge/usagemeter_test.go b/internal/edge/usagemeter_test.go| index 879f2905f..8ad8e0cd9 100644 |
| --- a/internal/edge/usagemeter_test.go |
| +++ b/internal/edge/usagemeter_test.go |
| @@ -6,6 +6,7 @@ import ( |
| "net/http/httptest" |
| "sync" |
| "testing" |
| + "time" |
| "github.com/carlosframework/platform/internal/edgeguard" |
| "github.com/carlosframework/platform/internal/ratelimit" |
| @@ -213,3 +214,23 @@ func TestRecordGuardOwnerChangeWithinDrain(t *testing.T) { |
| } |
| } |
| } |
| + |
| +func TestCountingWriterSupportsWriteDeadline(t *testing.T) { |
| + var n int64 |
| + errs := make(chan [2]error, 1) |
| + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| + cw := &countingWriter{ResponseWriter: w, n: &n} |
| + rc := http.NewResponseController(cw) |
| + errs <- [2]error{rc.SetWriteDeadline(time.Now().Add(time.Second)), rc.Flush()} |
| + })) |
| + defer srv.Close() |
| + resp, err := http.Get(srv.URL) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + resp.Body.Close() |
| + got := <-errs |
| + if got[0] != nil || got[1] != nil { |
| + t.Fatalf("SetWriteDeadline=%v Flush=%v, want nil nil", got[0], got[1]) |
| + } |
| +} |
diff --git a/internal/host/activator_seam.go b/internal/host/activator_seam.go| new file mode 100644 |
| index 000000000..a2ae10dc4 |
| --- /dev/null |
| +++ b/internal/host/activator_seam.go |
| @@ -0,0 +1,10 @@ |
| +package host |
| + |
| +import ( |
| + "github.com/carlosframework/platform/internal/activator" |
| + "github.com/carlosframework/platform/internal/edge" |
| +) |
| + |
| +// The activator is the edge's hibernation seam, including the held-leg |
| +// methods; this fails to compile if the two drift apart. |
| +var _ edge.Activator = (*activator.Service)(nil) |