carlos / platform Public

edge: case 3 bypasses the response cache (#186)

Task 10 as revised (spec §7.2 rev 5.1). A request whose selected upstream
carries a front mark (case 3: a front this box does not serve selected a
target here) neither looks up nor stores in the response LRU: one call,
pathLRUAllowed (pathhop.go), appended to ServeHTTP's cacheable condition.
A catch-all's cached page under the front never answers a case 3 request,
and nothing is stored there that only the target could purge. Purge code
is today's: Carlos-Purge-Host naming such a front is skipped as unserved.

unservedFrontTarget is extracted from case 3 selection unchanged and
pinned in selectionEntrySites. edge.go and ServeHTTP do not grow.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 56b808cd71f465999baae2312bf580061dc42595 parent 9927a60
5 files changed, +255 −12
  • internal/edge/edge.go +2 −2
  • internal/edge/linkpin_test.go +5 −2
  • internal/edge/path_across_cache_test.go +211 −0
  • internal/edge/pathhop.go +14 −0
  • internal/edge/pathselect.go +23 −8

CI cancelled — superseded by a newer push — run details

diff --git a/internal/edge/edge.go b/internal/edge/edge.go
index 59bd61717..a43f1c34b 100644
--- a/internal/edge/edge.go
+++ b/internal/edge/edge.go
@@ -2983,10 +2983,10 @@ func (e *Edge) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// (page cache design §5.3). This is the one place that rule is checked.
//
// A linked request is never cached, looked up or stored (ServeLinked),
- // as a forward row is not on the hub.
+ // as a forward row is not on the hub; nor is a case 3 target (pathLRUAllowed).
cacheable := e.cache != nil && (r.Method == http.MethodGet || r.Method == http.MethodHead) &&
up.route.Kind != "status" && up.route.Kind != "static" && up.route.ImportGate == "" &&
- !up.route.IsForward() && !markedRequest(r) && !e.pagesOptedIn(up) && !pinned
+ !up.route.IsForward() && !markedRequest(r) && !e.pagesOptedIn(up) && !pinned && pathLRUAllowed(up)
var key CacheKey
if cacheable {
key = CacheKey{Host: host, Method: r.Method, Path: r.URL.Path, RawQuery: r.URL.RawQuery}
diff --git a/internal/edge/linkpin_test.go b/internal/edge/linkpin_test.go
index 59b230286..c1775ae79 100644
--- a/internal/edge/linkpin_test.go
+++ b/internal/edge/linkpin_test.go
@@ -52,6 +52,9 @@ var selectionEntrySites = map[string]map[string]string{
"selectForUnservedFront": {
"selectAcross": "case 3, after the door and door-hop checks, with the remote map selectAcross read",
},
+ "unservedFrontTarget": {
+ "selectForUnservedFront": "case 3's eligibility of a target served here",
+ },
}
// The other ways to read the routing map by host, and where each may be
@@ -163,7 +166,7 @@ func TestSelectUpstreamIsTheOnlySite(t *testing.T) {
callers[fn.Name.Name] = append(callers[fn.Name.Name], fset.Position(ident.Pos()).String())
case "lookupIn", "lookupDemoAware":
note(ident.Name, fn.Name.Name, ident.Pos())
- case "pathMatch", "selectAcross", "selectFromServedFront", "selectForUnservedFront":
+ case "pathMatch", "selectAcross", "selectFromServedFront", "selectForUnservedFront", "unservedFrontTarget":
if ident == n {
entries[ident.Name] = appendSite(entries[ident.Name], fn.Name.Name, fset.Position(ident.Pos()).String())
}
@@ -179,7 +182,7 @@ func TestSelectUpstreamIsTheOnlySite(t *testing.T) {
if id, ok := n.(*ast.Ident); ok {
switch id.Name {
case "lookupUpstream", "lookupUpstreamPath", "lookupIn", "lookupDemoAware",
- "pathMatch", "selectAcross", "selectFromServedFront", "selectForUnservedFront":
+ "pathMatch", "selectAcross", "selectFromServedFront", "selectForUnservedFront", "unservedFrontTarget":
t.Errorf("%s: %s referenced outside a function", fset.Position(id.Pos()), id.Name)
}
}
diff --git a/internal/edge/path_across_cache_test.go b/internal/edge/path_across_cache_test.go
new file mode 100644
index 000000000..3d474195f
--- /dev/null
+++ b/internal/edge/path_across_cache_test.go
@@ -0,0 +1,211 @@
+package edge
+
+// Path routes across boxes (#186, spec §7.2, rev 5.1): a case 3 request (a
+// prefix of a front this box does not serve selects a target here) neither
+// looks up nor stores in the response LRU, and a target's Carlos-Purge-Host
+// naming that front is skipped as unserved, as it always was. Case 1 (front
+// and target both here) is cached as before.
+
+import (
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "sync/atomic"
+ "testing"
+
+ "github.com/carlosframework/platform/internal/registry"
+)
+
+// cacheAcrossBackend answers a GET with a cacheable body naming itself and
+// the request, counting them, and a POST with the purge declaration set on
+// it.
+type cacheAcrossBackend struct {
+ name string
+ gets atomic.Int64
+ purgeHost, purge string
+}
+
+func (b *cacheAcrossBackend) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+ if r.Method == http.MethodGet {
+ b.gets.Add(1)
+ w.Header().Set("Cache-Control", "public, max-age=600")
+ w.Write([]byte(b.name + "|" + r.Host + "|" + r.URL.Path))
+ return
+ }
+ if b.purgeHost != "" {
+ w.Header().Set("Carlos-Purge-Host", b.purgeHost)
+ }
+ if b.purge != "" {
+ w.Header().Set("Carlos-Purge", b.purge)
+ }
+ w.WriteHeader(http.StatusOK)
+}
+
+// backedBy makes b the backend of a route (its Addr).
+func backedBy(t *testing.T, b http.Handler) func(*registry.Route) {
+ t.Helper()
+ srv := httptest.NewServer(b)
+ t.Cleanup(srv.Close)
+ return func(r *registry.Route) { r.Addr = srv.Listener.Addr().String() }
+}
+
+// doAcross sends method host+urlPath through ServeHTTP and wants a 200.
+func doAcross(t *testing.T, e *Edge, method, host, urlPath string) *httptest.ResponseRecorder {
+ t.Helper()
+ rec := httptest.NewRecorder()
+ e.ServeHTTP(rec, httptest.NewRequest(method, "http://"+host+urlPath, nil))
+ if rec.Code != http.StatusOK {
+ t.Fatalf("%s %s%s = %d, want 200; body %q", method, host, urlPath, rec.Code, rec.Body.String())
+ }
+ return rec
+}
+
+// case3Scene is a box serving the target tenant.example.com (account bae)
+// from tenant, with the claim's row acme -> tenant.example.com for
+// hub.example.com, which betty publishes as a front route of bae/hub. The
+// cache is armed. extra runs before the Edge is built.
+func case3Scene(t *testing.T, tenant http.Handler, extra func(*selScene)) (*selScene, *Edge) {
+ t.Helper()
+ s := newSelScene(t)
+ s.instance(selTarget, selAcct, "tenant", backedBy(t, tenant))
+ s.path(selFront, "acme", selTarget, selAcct, "hub")
+ s.publish("betty", selFront, frontRow(selAcct, "hub"))
+ if extra != nil {
+ extra(s)
+ }
+ e := s.build()
+ e.EnableCache(NewCache(DefaultCacheMaxEntries, DefaultCacheMaxBodyBytes))
+ got := s.sel(selFront, "/acme/page")
+ wantLocal(t, got, selTarget, "acme", selTarget)
+ if got.Up.front == nil {
+ t.Fatalf("fixture: %s is not a case 3 selection", describeSel(got))
+ }
+ return s, e
+}
+
+func TestPathAcrossCase3NotCached(t *testing.T) {
+ tenant := &cacheAcrossBackend{name: "tenant"}
+ _, e := case3Scene(t, tenant, nil)
+
+ for range 2 {
+ doAcross(t, e, http.MethodGet, selFront, "/acme/page")
+ }
+ if n := tenant.gets.Load(); n != 2 {
+ t.Fatalf("the target saw %d GETs for two identical case 3 requests, want 2", n)
+ }
+ if cached(e.cache, selFront, "/acme/page", "") {
+ t.Fatal("a cacheable case 3 response was stored under the front")
+ }
+ // Control: the target's own host is cached on the same box.
+ for range 2 {
+ doAcross(t, e, http.MethodGet, selTarget, "/page")
+ }
+ if n := tenant.gets.Load(); n != 3 {
+ t.Fatalf("control: the target saw %d GETs, want 3 (its own host's second GET a hit)", n)
+ }
+}
+
+// A catch-all here (*.example.com, another account) answered hub.example.com
+// before the claim's path rows arrived, and its page is cached under that
+// name. Once case 3 selects the target for the prefix, the request reaches
+// the target, never the catch-all's cached page.
+func TestPathAcrossCase3NeverServedFromCatchAllCache(t *testing.T) {
+ tenant := &cacheAcrossBackend{name: "tenant"}
+ wild := &cacheAcrossBackend{name: "wild"}
+ s := newSelScene(t)
+ s.instance(selTarget, selAcct, "tenant", backedBy(t, tenant))
+ s.instance("wild.example.net", selOther, "wild", backedBy(t, wild))
+ if err := s.reg.UpsertAlias(registry.RouteAlias{Alias: "*.example.com", Host: "wild.example.net", Account: selOther, App: "wild"}); err != nil {
+ t.Fatal(err)
+ }
+ s.publish("betty", selFront, frontRow(selAcct, "hub"))
+ e := s.build()
+ e.EnableCache(NewCache(DefaultCacheMaxEntries, DefaultCacheMaxBodyBytes))
+
+ for range 2 {
+ doAcross(t, e, http.MethodGet, selFront, "/acme/page")
+ }
+ if n := wild.gets.Load(); n != 1 || !cached(e.cache, selFront, "/acme/page", "") {
+ t.Fatalf("fixture: the catch-all saw %d GETs (want 1) and its page cached = %v (want true)", n, cached(e.cache, selFront, "/acme/page", ""))
+ }
+
+ s.path(selFront, "acme", selTarget, selAcct, "hub")
+ if err := e.Reload(); err != nil {
+ t.Fatal(err)
+ }
+ got := s.sel(selFront, "/acme/page")
+ wantLocal(t, got, selTarget, "acme", selTarget)
+ if got.Up.front == nil {
+ t.Fatalf("fixture: %s is not a case 3 selection", describeSel(got))
+ }
+ for i := range 2 {
+ rec := doAcross(t, e, http.MethodGet, selFront, "/acme/page")
+ if want := "tenant|" + selFront + "|/acme/page"; rec.Body.String() != want {
+ t.Fatalf("request %d: body %q, want the target's %q (never the catch-all's cached page)", i, rec.Body.String(), want)
+ }
+ }
+ if n := tenant.gets.Load(); n != 2 {
+ t.Fatalf("the target saw %d GETs, want 2", n)
+ }
+}
+
+// Case 1: the front and the target both here. The front's prefix is cached
+// under the front's name as it always was.
+func TestPathAcrossSameBoxStillCached(t *testing.T) {
+ tenant := &cacheAcrossBackend{name: "tenant"}
+ s := newSelScene(t)
+ s.instance(selFront, selAcct, "hub")
+ s.instance(selTarget, selAcct, "tenant", backedBy(t, tenant))
+ s.path(selFront, "acme", selTarget, selAcct, "hub")
+ e := s.build()
+ e.EnableCache(NewCache(DefaultCacheMaxEntries, DefaultCacheMaxBodyBytes))
+ got := s.sel(selFront, "/acme/page")
+ wantLocal(t, got, selTarget, "acme", selTarget)
+ if got.Up.front != nil {
+ t.Fatalf("fixture: a same-box target carries a front mark: %s", describeSel(got))
+ }
+
+ for range 2 {
+ doAcross(t, e, http.MethodGet, selFront, "/acme/page")
+ }
+ if n := tenant.gets.Load(); n != 1 {
+ t.Fatalf("the target saw %d GETs, want 1 (the second answered from the LRU)", n)
+ }
+ if !cached(e.cache, selFront, "/acme/page", "") {
+ t.Fatal("a case 1 response was not stored under the front")
+ }
+}
+
+// A target naming a front this box does not serve in Carlos-Purge-Host: the
+// name is skipped as unserved, nothing under it is evicted, the rest of the
+// declaration (the target's own host) is honoured, and the write itself
+// succeeds, sent on the front (case 3) or on the target's own host.
+func TestPurgeAcrossSkipped(t *testing.T) {
+ for _, on := range []struct{ host, path string }{
+ {selFront, "/acme/admin"},
+ {selTarget, "/admin"},
+ } {
+ t.Run(on.host, func(t *testing.T) {
+ logs := captureLog(t)
+ tenant := &cacheAcrossBackend{name: "tenant", purgeHost: selFront + ", " + selTarget, purge: "*"}
+ _, e := case3Scene(t, tenant, nil)
+ seed(t, e.cache, selFront, "/acme/events/42", "", "under the front")
+ seed(t, e.cache, selTarget, "/events/42", "", "the target's own")
+
+ rec := doAcross(t, e, http.MethodPost, on.host, on.path)
+
+ if got := rec.Header().Get("Carlos-Purge-Host"); got != "" {
+ t.Fatalf("Carlos-Purge-Host reached the client: %q", got)
+ }
+ if !cached(e.cache, selFront, "/acme/events/42", "") {
+ t.Fatal("a purge naming a front this box does not serve evicted an entry under it")
+ }
+ if cached(e.cache, selTarget, "/events/42", "") {
+ t.Fatal("the rest of the declaration (the target's own host) was not honoured")
+ }
+ if out := logs.String(); !strings.Contains(out, "does not serve") {
+ t.Errorf("no unserved line for the skipped front; log:\n%s", out)
+ }
+ })
+ }
+}
diff --git a/internal/edge/pathhop.go b/internal/edge/pathhop.go
index 560423685..ede50ed25 100644
--- a/internal/edge/pathhop.go
+++ b/internal/edge/pathhop.go
@@ -88,6 +88,20 @@ func (e *Edge) servePathRemote(w http.ResponseWriter, r *http.Request, host stri
return true
}
+// pathLRUAllowed reports whether a request dispatched to up may use the
+// response LRU at all, in either direction (spec §7.2, rev 5.1): never for a
+// case 3 target (up.front set: a front this box does not serve selected a
+// target here). Its responses would be keyed on the front's name, which on
+// this box may also be answered by a catch-all of another tenancy, and no
+// purge here can reach them: Carlos-Purge-Host naming a front this box does
+// not serve is skipped as unserved, as it always was. Not looking up keeps a
+// catch-all's entry under the front from ever answering a case 3 request;
+// not storing leaves nothing under the front that only the target could
+// purge. Case 1 (front and target both here) is cached as before.
+func pathLRUAllowed(up upstream) bool {
+ return up.front == nil
+}
+
// frontMarkHolds reports whether up is a case 3 target (selectAcross: a front
// this box does not serve) whose front mark names host and still matches the
// home map's exact row for host: a front route with the same account, app,
diff --git a/internal/edge/pathselect.go b/internal/edge/pathselect.go
index 4fefacc1b..0689c44da 100644
--- a/internal/edge/pathselect.go
+++ b/internal/edge/pathselect.go
@@ -435,14 +435,9 @@ func (e *Edge) selectForUnservedFront(t *routeTable, host, urlPath string, rm re
// refusal; an unserved front's refusal reports what matched.
stay.Prefix, stay.Target = prefix, target
}
- if tu, here := t.hosts[target]; here {
- // A platform-alias target is its instance (selectedEntry), so the
- // box holding I's lease re-selects I for a hop another box sent
- // under I (spec §5.7.3); every other entry must be target's own.
- named := !tu.platformAlias
- tu, ok := selectedEntry(t.hosts, tu)
- if !ok || tu.alias || tu.regional != regionalNone || (named && tu.route.Host != target) ||
- !registry.PathTarget(tu.route) || release.RouteAccount(tu.route) != rm.account {
+ if _, here := t.hosts[target]; here {
+ tu, ok := unservedFrontTarget(t.hosts, target, rm.account)
+ if !ok {
e.pathSkip(host, fmt.Sprintf("prefix %q names %s, which this box serves but not as a ready instance or static route of account %s; sent to the host's home", prefix, target, rm.account))
return stay
}
@@ -457,6 +452,26 @@ func (e *Edge) selectForUnservedFront(t *routeTable, host, urlPath string, rm re
return requestSelection{Remote: &remoteTarget{Target: target, Route: info}, Prefix: prefix, Target: target}
}
+// unservedFrontTarget is the entry case 3 dispatches a prefix of an unserved
+// front to when its target is served here: the target's own entry, or for a
+// platform-alias target its instance's (selectedEntry), so the box holding
+// I's lease re-selects I for a hop another box sent under I (spec §5.7.3).
+// ok only for a route of account satisfying registry.PathTarget that is not
+// an attached alias or a regional entry.
+func unservedFrontTarget(hosts map[string]upstream, target, account string) (upstream, bool) {
+ tu, here := hosts[target]
+ if !here {
+ return upstream{}, false
+ }
+ named := !tu.platformAlias
+ tu, ok := selectedEntry(hosts, tu)
+ if !ok || tu.alias || tu.regional != regionalNone || (named && tu.route.Host != target) ||
+ !registry.PathTarget(tu.route) || release.RouteAccount(tu.route) != account {
+ return upstream{}, false
+ }
+ return tu, true
+}
+
// servedName is the key t.hosts answered host under: host itself, or its
// one-label catch-all. A log key, so it is never the request's own spelling
// of a name only a catch-all serves (logThrottled: never key from remote