carlos / platform Public

region-ha p1b-2: forward review fixes: only the platform names a holder, no transport gzip, a real HEAD

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 58c9420ed4abdc71c3691c7e7501deb9682fe130 parent 84b5229
4 files changed, +137 −22
  • internal/edge/assetcatch.go +3 −0
  • internal/edge/cookiescope.go +8 −2
  • internal/edge/forwardha.go +10 −4
  • internal/edge/forwardha_test.go +116 −16
diff --git a/internal/edge/assetcatch.go b/internal/edge/assetcatch.go
index d082efa33..9add65556 100644
--- a/internal/edge/assetcatch.go
+++ b/internal/edge/assetcatch.go
@@ -1275,6 +1275,9 @@ type assetInformationalScrub struct{ http.ResponseWriter }
func (s assetInformationalScrub) WriteHeader(code int) {
if code < 200 {
stripAssetResponseHeaders(s.Header())
+ // Nor an app's holder diagnostic (responseModifier's rule): the
+ // platform never sends one on a 1xx.
+ s.Header().Del(holderHeader)
}
s.ResponseWriter.WriteHeader(code)
}
diff --git a/internal/edge/cookiescope.go b/internal/edge/cookiescope.go
index e8f073124..72af4d824 100644
--- a/internal/edge/cookiescope.go
+++ b/internal/edge/cookiescope.go
@@ -74,8 +74,13 @@ func appsDomainFromEnv() string {
// proxy: the Carlos-Purge channel (purgeModifier), the removal of any asset
// cache header or trailer the app set (stripAssetResponseHeaders,
// stripAssetTrailers: the edge's own are set after this, never inherited
-// from the app) and the cookie scoping above, in that order. None depends on
-// another.
+// from the app), the removal of any X-Carlos-Holder the app set, and the
+// cookie scoping above, in that order. None depends on another.
+//
+// X-Carlos-Holder names the box holding an HA host's lease (forwardha.go),
+// and a forwarding box's holder cache follows it, so only the platform may
+// say it: the activator's and the edge's own refusals, which are written to
+// the client directly and never pass through this hook.
func (e *Edge) responseModifier(rt registry.Route) func(*http.Response) error {
purge := e.purgeModifier(rt)
return func(resp *http.Response) error {
@@ -84,6 +89,7 @@ func (e *Edge) responseModifier(rt registry.Route) func(*http.Response) error {
}
stripAssetResponseHeaders(resp.Header)
stripAssetTrailers(resp)
+ resp.Header.Del(holderHeader)
e.scopeCookies(rt, resp)
return nil
}
diff --git a/internal/edge/forwardha.go b/internal/edge/forwardha.go
index 54c47fcb8..ea9bd9391 100644
--- a/internal/edge/forwardha.go
+++ b/internal/edge/forwardha.go
@@ -65,7 +65,8 @@ type ForwardResult struct {
}
// holderHeader is the holder's diagnostic on an answer to a forwarded
-// request: the box that holds the lease, or "none".
+// request: the box that holds the lease, or "none". Only the platform sets
+// it; an app's is removed at the tenant response boundary (responseModifier).
const holderHeader = "X-Carlos-Holder"
// forwardDialTimeout bounds connecting to the holder, TCP and TLS together.
@@ -196,9 +197,14 @@ func (e *Edge) forwardTransport(dial string) *http.Transport {
return conn, nil
},
ResponseHeaderTimeout: backhaulHeaderTimeout,
- MaxIdleConns: 32,
- MaxIdleConnsPerHost: 32,
- IdleConnTimeout: 90 * time.Second,
+ // The client's Accept-Encoding goes to the holder as sent. Without
+ // this the transport would ask for gzip on the client's behalf and
+ // undo it here, and the holder would meter the compressed bytes
+ // while this box delivered more.
+ DisableCompression: true,
+ MaxIdleConns: 32,
+ MaxIdleConnsPerHost: 32,
+ IdleConnTimeout: 90 * time.Second,
}
if existing, loaded := e.forwardTransports.LoadOrStore(dial, tr); loaded {
return existing.(*http.Transport)
diff --git a/internal/edge/forwardha_test.go b/internal/edge/forwardha_test.go
index 9e5189081..92c6a45bb 100644
--- a/internal/edge/forwardha_test.go
+++ b/internal/edge/forwardha_test.go
@@ -53,6 +53,9 @@ const (
fwPassword = "correct horse"
)
+// fwBigBody is a page large and repetitive enough for the edge to gzip.
+var fwBigBody = strings.Repeat("<p>forwarded and compressible</p>\n", 2048)
+
var fwHosts = []string{fwHost, fwGatedHost, fwAssetHost, fwRateHost, fwLegacyHost}
// fwAct is a box's activator: it wakes nothing (the route's TCP upstream
@@ -71,7 +74,16 @@ type fwNote struct {
var _ HAForwarder = (*fwAct)(nil)
-func (a *fwAct) ServeHTTP(http.ResponseWriter, *http.Request, registry.Route, http.Handler) bool {
+func (a *fwAct) ServeHTTP(w http.ResponseWriter, r *http.Request, _ registry.Route, _ http.Handler) bool {
+ // The platform's own refusal of a forwarded request (Task 3's answer):
+ // written by the activator, never by the app.
+ if r.URL.Path == "/platform-held" {
+ w.Header().Set(holderHeader, r.URL.Query().Get("ref"))
+ w.Header().Set("Retry-After", "1")
+ MarkAnswered(r)
+ http.Error(w, "instance is waking elsewhere; retry shortly", http.StatusServiceUnavailable)
+ return true
+ }
return false
}
func (a *fwAct) RetireExcept(map[string]bool) {}
@@ -101,7 +113,8 @@ func (fwPlainAct) RetireExcept(map[string]bool) {}
// 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
-// X-Carlos-Holder set to its ref query (none when the query is empty);
+// X-Carlos-Holder set to its ref query (none when the query is empty), or
+// the status query's status; /big is a large compressible page;
// /assets/ is a cacheable script, and /assets/held.js waits on the first
// request until the test releases it; an upgrade echoes with the box's name.
type fwAppServer struct {
@@ -130,8 +143,15 @@ func fwApp(t *testing.T, box string) *fwAppServer {
if ref := r.URL.Query().Get("ref"); ref != "" {
w.Header().Set(holderHeader, ref)
}
+ status := http.StatusServiceUnavailable
+ if r.URL.Query().Get("status") == "200" {
+ status = http.StatusOK
+ }
w.Header().Set("Retry-After", "1")
- http.Error(w, "held", http.StatusServiceUnavailable)
+ http.Error(w, "held", status)
+ case r.URL.Path == "/big":
+ w.Header().Set("Content-Type", "text/html; charset=utf-8")
+ io.WriteString(w, fwBigBody)
case r.URL.Path == "/assets/held.js":
a.heldRequests.Add(1)
if a.first() {
@@ -302,7 +322,29 @@ func fwDialTo(t *testing.T, addr string) {
func (x *fwScene) fetch(t *testing.T, host, path string, hdr ...string) (*http.Response, string) {
t.Helper()
- return x.get(t, x.a, "https://"+host+path, e2eHeaders(hdr), true)
+ return x.do(t, http.MethodGet, host, path, hdr...)
+}
+
+// do is fetch with the method given, through annie's client.
+func (x *fwScene) do(t *testing.T, method, host, path string, hdr ...string) (*http.Response, string) {
+ t.Helper()
+ req, err := http.NewRequest(method, "https://"+host+path, nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+ for k, v := range e2eHeaders(hdr) {
+ req.Header.Set(k, v)
+ }
+ resp, err := x.a.client.Do(req)
+ if err != nil {
+ t.Fatalf("%s %s%s: %v", method, host, path, err)
+ }
+ b, err := io.ReadAll(resp.Body)
+ resp.Body.Close()
+ if err != nil {
+ t.Fatal(err)
+ }
+ return resp, string(b)
}
func (x *fwScene) fetchAsync(host, path string) <-chan e2eResult {
@@ -442,27 +484,33 @@ func TestFrontForwardRunsNoLocalStep(t *testing.T) {
})
t.Run("b assets", func(t *testing.T) {
- step := func(name, path string, wantStatus int, hdr ...string) *http.Response {
+ step := func(name, method, path string, wantStatus int, hdr ...string) *http.Response {
t.Helper()
- resp, body := x.fetch(t, fwAssetHost, path, hdr...)
+ resp, body := x.do(t, method, fwAssetHost, path, hdr...)
if resp.StatusCode != wantStatus {
t.Fatalf("%s = %d %q, want %d", name, resp.StatusCode, body, wantStatus)
}
+ if method == http.MethodHead && body != "" {
+ t.Fatalf("HEAD body = %q, want none", body)
+ }
annie, betty := x.take(t, fwAssetHost)
wantNothing(t, name, annie)
wantOneDelivery(t, name, betty, len(body), wantStatus)
+ if got := betty.obs[0].Method; got != method {
+ t.Fatalf("%s: the holder observed a %s", name, got)
+ }
return resp
}
// Carlos-Cache does not cross a hop (scrubBackhaulResponse), so the
// catch is read off betty's store and app.
- get := step("GET", "/assets/app.js", http.StatusOK)
+ get := step("GET", http.MethodGet, "/assets/app.js", http.StatusOK)
appKey := assetcache.Key{Account: "abc", App: "shop", Gen: laGen, Host: fwAssetHost, Path: "/assets/app.js"}
if _, ok := x.bStore.Get(appKey); !ok {
t.Fatal("betty's asset cache did not catch the GET")
}
- step("HEAD", "/assets/app.js", http.StatusOK)
- step("range", "/assets/app.js", http.StatusPartialContent, "Range", "bytes=0-4")
- step("conditional", "/assets/app.js", http.StatusNotModified, "If-None-Match", get.Header.Get("ETag"))
+ step("HEAD", http.MethodHead, "/assets/app.js", http.StatusOK)
+ step("range", http.MethodGet, "/assets/app.js", http.StatusPartialContent, "Range", "bytes=0-4")
+ step("conditional", http.MethodGet, "/assets/app.js", http.StatusNotModified, "If-None-Match", get.Header.Get("ETag"))
if n := x.bApp.assetRequests.Load(); n != 1 {
t.Fatalf("betty's app saw %d asset requests, want the one fill", n)
}
@@ -777,18 +825,70 @@ func TestForwardHADialFailureIs503RetryTen(t *testing.T) {
func TestForwardHAReadsHolderHeader(t *testing.T) {
x := newFwScene(t, fwOpts{})
- for i, ref := range []string{"carol", "none"} {
- resp, _ := x.fetch(t, fwHost, "/holder?ref="+ref)
+ n := 0
+ note := func(t *testing.T, want ForwardResult) {
+ t.Helper()
+ n++
+ notes := x.aAct.noted()
+ if len(notes) != n || notes[n-1] != (fwNote{fwHost, want}) {
+ t.Fatalf("notes = %+v, want %+v last", notes, want)
+ }
+ }
+ // The platform's refusal on the holder keeps its header, to the client
+ // and to the note.
+ for _, ref := range []string{"carol", "none"} {
+ resp, _ := x.fetch(t, fwHost, "/platform-held?ref="+ref)
if resp.StatusCode != http.StatusServiceUnavailable || resp.Header.Get(holderHeader) != ref {
- t.Fatalf("GET = %d %s %q, want betty's 503 naming %s", resp.StatusCode, holderHeader, resp.Header.Get(holderHeader), ref)
+ t.Fatalf("platform refusal = %d %s %q, want betty's 503 naming %s", resp.StatusCode, holderHeader, resp.Header.Get(holderHeader), ref)
}
- notes := x.aAct.noted()
- if len(notes) != i+1 || notes[i] != (fwNote{fwHost, ForwardResult{Outcome: ForwardDone, Status: http.StatusServiceUnavailable, Holder: ref}}) {
- t.Fatalf("notes = %+v, want ForwardDone 503 %q", notes, ref)
+ note(t, ForwardResult{Outcome: ForwardDone, Status: http.StatusServiceUnavailable, Holder: ref})
+ }
+ // An app's own X-Carlos-Holder never leaves the holder: not to the
+ // client, not into the note.
+ for _, tc := range []struct {
+ query string
+ status int
+ }{{"ref=carol", http.StatusServiceUnavailable}, {"ref=none&status=200", http.StatusOK}} {
+ resp, body := x.fetch(t, fwHost, "/holder?"+tc.query)
+ if resp.StatusCode != tc.status || body != "held\n" {
+ t.Fatalf("app %s = %d %q, want the app's %d", tc.query, resp.StatusCode, body, tc.status)
+ }
+ if v := resp.Header.Values(holderHeader); len(v) != 0 {
+ t.Fatalf("app %s: the client received %s %q", tc.query, holderHeader, v)
}
+ note(t, ForwardResult{Outcome: ForwardDone, Status: tc.status})
}
}
+// The forward relays the encoding the client asked for, so the bytes the
+// holder meters are the bytes the client receives: a client that asks for
+// no compression gets identity bytes from both boxes.
+func TestForwardHAHolderMetersTheBytesDelivered(t *testing.T) {
+ x := newFwScene(t, fwOpts{})
+ tr := x.a.client.Transport.(*http.Transport).Clone()
+ tr.DisableCompression = true
+ client := &http.Client{Timeout: 30 * time.Second, Transport: tr}
+ req, err := http.NewRequest(http.MethodGet, "https://"+fwHost+"/big", nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+ resp, err := client.Do(req)
+ if err != nil {
+ t.Fatal(err)
+ }
+ b, err := io.ReadAll(resp.Body)
+ resp.Body.Close()
+ if err != nil {
+ t.Fatal(err)
+ }
+ if resp.StatusCode != http.StatusOK || string(b) != fwBigBody || resp.Header.Get("Content-Encoding") != "" {
+ t.Fatalf("GET = %d, %d bytes, Content-Encoding %q; want the identity page", resp.StatusCode, len(b), resp.Header.Get("Content-Encoding"))
+ }
+ annie, betty := x.take(t, fwHost)
+ wantNothing(t, "a large page", annie)
+ wantOneDelivery(t, "a large page", betty, len(b), http.StatusOK)
+}
+
func TestForwardHAUpgradeSwitchesProtocols(t *testing.T) {
x := newFwScene(t, fwOpts{})
raw, err := net.DialTimeout("tcp", x.a.tlsAddr, 10*time.Second)