edge: a path target purges its pages under the front host (#186)
Task 10 (spec §7.2). A target serving a front this box does not serve (case 3) caches its pages under the front host; Carlos-Purge-Host naming that front was skipped as unserved. purgeOwnsIn (replacing purgeOwns) and resolvePurgeOwnersIn now take the *routeTable and, only for a host t.hosts does not resolve at all, accept it via frontPathOwns: the owner is in the claim's account, the home map's exact row is a front route of the claim's account and app, and a prefix of the remote map selects the owner here (unservedFrontTarget, extracted from case 3 selection, so a platform-alias target is its instance). Every host this box resolves is judged exactly as before. ownsHost reads the same table. Tests: exact URL, path, "*"/"all" evictions end to end; refusals after an in-flight prefix move, removal or instance replacement, an unpublished or mismatched front row, another account, an unselected route.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
6 files changed,
+437
−49
internal/edge/cache.go+1 −1internal/edge/linkpin_test.go+6 −6internal/edge/pagecache.go+3 −2internal/edge/pathselect.go+25 −8internal/edge/purge_across_test.go+294 −0internal/edge/purge_header.go+108 −32
diff --git a/internal/edge/cache.go b/internal/edge/cache.go| index ebdc1dda9..a43c25011 100644 |
| --- a/internal/edge/cache.go |
| +++ b/internal/edge/cache.go |
| @@ -122,7 +122,7 @@ type purgeBridge interface { |
| // a wildcard alias). |
| instanceHosts(owner registry.Route, cached []string) []string |
| // ownsHost reports whether owner still owns host in the live table, so |
| - // a purge acting for owner may touch host's LRU entries (purgeOwns). |
| + // a purge acting for owner may touch host's LRU entries (purgeOwnsIn). |
| ownsHost(owner registry.Route, host string) bool |
| } |
diff --git a/internal/edge/linkpin_test.go b/internal/edge/linkpin_test.go| index 59b230286..cc95bd49d 100644 |
| --- a/internal/edge/linkpin_test.go |
| +++ b/internal/edge/linkpin_test.go |
| @@ -67,7 +67,7 @@ var mapReadSites = map[string]map[string]string{ |
| "pageRouteEligible": "the page cache's eligibility of the upstream already selected; never reached by a linked request", |
| "pageHostsFor": "the pages a purge declaration clears, from the response's own route", |
| "resolvePurgeOwnersIn": "validates the hosts a Carlos-Purge-Host names against the serving route's tenancy", |
| - "purgeOwns": "the owner check of a purge target host", |
| + "purgeOwnsIn": "the owner check of a purge target host", |
| }, |
| // lookupDemoAware(m, host): the demo zone's exact-only lookup. |
| "lookupDemoAware": { |
| @@ -82,12 +82,12 @@ var mapReadSites = map[string]map[string]string{ |
| "takeHost": "the HA take control path, an edge-owned handler a linked request never reaches", |
| }, |
| // routeTable.remote: the remote path map (pathremote.go), read or set. |
| - // The purge owner check for a front this box does not serve |
| - // (purgeOwnsIn, resolvePurgeOwnersIn) joins when it reads the map. |
| "routeTable.remote": { |
| - "selectAcross": "case 3 of the one selection, reached only from selectUpstream", |
| - "selectUpstream": "THE selection: whether a host it answers only through a catch-all has a remote map, so case 3 runs for it", |
| - "Reload": "the one publisher of the table (TestReloadIsTheOnlyPublisher) sets it beside hosts", |
| + "selectAcross": "case 3 of the one selection, reached only from selectUpstream", |
| + "selectUpstream": "THE selection: whether a host it answers only through a catch-all has a remote map, so case 3 runs for it", |
| + "Reload": "the one publisher of the table (TestReloadIsTheOnlyPublisher) sets it beside hosts", |
| + "purgeOwnsIn": "the LRU owner check of a purge for a front this box does not serve (spec §7.2); it can only refuse or allow an eviction, never pick an upstream", |
| + "resolvePurgeOwnersIn": "accepts a Carlos-Purge-Host naming a front this box does not serve whose prefix selects the serving route (spec §7.2); never picks an upstream", |
| }, |
| } |
diff --git a/internal/edge/pagecache.go b/internal/edge/pagecache.go| index 7526d9e2f..5c8c64359 100644 |
| --- a/internal/edge/pagecache.go |
| +++ b/internal/edge/pagecache.go |
| @@ -164,9 +164,10 @@ func (e *Edge) instanceHosts(owner registry.Route, cached []string) []string { |
| return instanceHostsIn(e.current(), owner, cached) |
| } |
| -// ownsHost is purgeOwns over one snapshot of the live table (purgeBridge). |
| +// ownsHost is purgeOwnsIn over one snapshot of the live table, its remote |
| +// path map included (purgeBridge). |
| func (e *Edge) ownsHost(owner registry.Route, host string) bool { |
| - return purgeOwns(e.current(), owner, host) |
| + return e.purgeOwnsIn(e.purgeTable(), owner, host) |
| } |
| // instanceHostsIn is every host m serves as owner's instance, sorted: each |
diff --git a/internal/edge/pathselect.go b/internal/edge/pathselect.go| index 4fefacc1b..982b16085 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,28 @@ 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. The purge check for the front |
| +// (frontPathOwns) asks the same question, so a route purges the front's |
| +// pages exactly while a prefix of the front selects it. |
| +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 |
diff --git a/internal/edge/purge_across_test.go b/internal/edge/purge_across_test.go| new file mode 100644 |
| index 000000000..bb615ab94 |
| --- /dev/null |
| +++ b/internal/edge/purge_across_test.go |
| @@ -0,0 +1,294 @@ |
| +package edge |
| + |
| +// Path routes across boxes (#186, spec §7.2): purge for a front host this box |
| +// does not serve. A prefix on hub.example.com (served on betty) selects |
| +// tenant.example.com, served here (case 3), so this box caches the tenant's |
| +// pages under hub.example.com. The tenant purges them by naming that host in |
| +// Carlos-Purge-Host, and the edge honours it only while this box's own |
| +// snapshot still says the prefix selects the purging route, the front is a |
| +// published front route of the claim's account and app, and the purging |
| +// route is the instance served here. |
| + |
| +import ( |
| + "net/http" |
| + "net/http/httptest" |
| + "testing" |
| + |
| + "github.com/carlosframework/platform/internal/registry" |
| +) |
| + |
| +// purgeAcrossBackend answers every request with the purge declaration |
| +// purge, naming the front host, after running before (when set) while the |
| +// request is in flight. |
| +type purgeAcrossBackend struct { |
| + purge string |
| + before func() |
| +} |
| + |
| +func (b *purgeAcrossBackend) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| + if b.before != nil { |
| + b.before() |
| + } |
| + w.Header().Set("Carlos-Purge-Host", selFront) |
| + w.Header().Set("Carlos-Purge", b.purge) |
| + w.WriteHeader(http.StatusOK) |
| +} |
| + |
| +// purgeAcrossScene builds this box: the target route tenant.example.com of |
| +// account (app "tenant") served by b, the claim's row acme -> |
| +// tenant.example.com for hub.example.com (account bae, app hub), and |
| +// hub.example.com published on betty as a front route of bae/hub. The cache |
| +// is armed. |
| +func purgeAcrossScene(t *testing.T, account string, b *purgeAcrossBackend) (*selScene, *Edge) { |
| + t.Helper() |
| + backend := httptest.NewServer(b) |
| + t.Cleanup(backend.Close) |
| + s := newSelScene(t) |
| + s.instance(selTarget, account, "tenant", func(r *registry.Route) { r.Addr = backend.Listener.Addr().String() }) |
| + s.path(selFront, "acme", selTarget, selAcct, "hub") |
| + s.publish("betty", selFront, frontRow(selAcct, "hub")) |
| + e := s.build() |
| + e.EnableCache(NewCache(DefaultCacheMaxEntries, DefaultCacheMaxBodyBytes)) |
| + return s, e |
| +} |
| + |
| +// postAcross sends a write on host+urlPath through ServeHTTP and wants it |
| +// served (200) with the purge headers stripped. |
| +func postAcross(t *testing.T, e *Edge, host, urlPath string) { |
| + t.Helper() |
| + rec := httptest.NewRecorder() |
| + e.ServeHTTP(rec, httptest.NewRequest(http.MethodPost, "http://"+host+urlPath, nil)) |
| + if rec.Code != http.StatusOK { |
| + t.Fatalf("POST %s%s = %d, want 200 (served by the target here); body %q", host, urlPath, rec.Code, rec.Body.String()) |
| + } |
| + if got := rec.Header().Get("Carlos-Purge-Host"); got != "" { |
| + t.Fatalf("Carlos-Purge-Host reached the client: %q", got) |
| + } |
| +} |
| + |
| +// wantCase3 pins the precondition: a request for the front's prefix is a |
| +// case 3 local target here, so its responses are cached under the front. |
| +func wantCase3(t *testing.T, s *selScene) { |
| + t.Helper() |
| + got := s.sel(selFront, "/acme/admin") |
| + wantLocal(t, got, selTarget, "acme", selTarget) |
| + if got.Up.front == nil { |
| + t.Fatalf("precondition: selection %s carries no front mark: not case 3", describeSel(got)) |
| + } |
| +} |
| + |
| +func wantCached(t *testing.T, e *Edge, host, path, rawQuery string, want bool, why string) { |
| + t.Helper() |
| + if got := cached(e.cache, host, path, rawQuery); got != want { |
| + t.Fatalf("%s%s?%s cached = %v, want %v: %s", host, path, rawQuery, got, want, why) |
| + } |
| +} |
| + |
| +func TestPurgeAcrossEvictsExactURL(t *testing.T) { |
| + s, e := purgeAcrossScene(t, selAcct, &purgeAcrossBackend{purge: "/acme/events/42?lang=fr"}) |
| + wantCase3(t, s) |
| + seed(t, e.cache, selFront, "/acme/events/42", "lang=fr", "stale") |
| + seed(t, e.cache, selFront, "/acme/events/42", "", "untouched") |
| + |
| + postAcross(t, e, selFront, "/acme/admin") |
| + |
| + wantCached(t, e, selFront, "/acme/events/42", "lang=fr", false, "the target could not purge its own page under the front host") |
| + wantCached(t, e, selFront, "/acme/events/42", "", true, "an exact-URL purge dropped another query") |
| +} |
| + |
| +func TestPurgeAcrossEvictsPath(t *testing.T) { |
| + s, e := purgeAcrossScene(t, selAcct, &purgeAcrossBackend{purge: "/acme/events/42"}) |
| + wantCase3(t, s) |
| + seed(t, e.cache, selFront, "/acme/events/42", "", "stale") |
| + seed(t, e.cache, selFront, "/acme/events/42", "lang=fr", "stale") |
| + seed(t, e.cache, selFront, "/acme/events/43", "", "untouched") |
| + |
| + postAcross(t, e, selFront, "/acme/admin") |
| + |
| + wantCached(t, e, selFront, "/acme/events/42", "", false, "the path purge missed the page") |
| + wantCached(t, e, selFront, "/acme/events/42", "lang=fr", false, "the path purge missed a query variant") |
| + wantCached(t, e, selFront, "/acme/events/43", "", true, "a path purge dropped another path") |
| +} |
| + |
| +func TestPurgeAcrossEvictsWholeHost(t *testing.T) { |
| + for _, purge := range []string{"*", "all"} { |
| + t.Run(purge, func(t *testing.T) { |
| + s, e := purgeAcrossScene(t, selAcct, &purgeAcrossBackend{purge: purge}) |
| + wantCase3(t, s) |
| + seed(t, e.cache, selFront, "/acme/events/42", "", "stale") |
| + seed(t, e.cache, selFront, "/acme/", "", "stale") |
| + seed(t, e.cache, "bystander.example.com", "/acme/events/42", "", "untouched") |
| + |
| + postAcross(t, e, selFront, "/acme/admin") |
| + |
| + wantCached(t, e, selFront, "/acme/events/42", "", false, "the whole-host purge missed a page") |
| + wantCached(t, e, selFront, "/acme/", "", false, "the whole-host purge missed a page") |
| + wantCached(t, e, "bystander.example.com", "/acme/events/42", "", true, "a whole-host purge reached a host nobody named") |
| + }) |
| + } |
| +} |
| + |
| +// The prefix moves to another target while the write is in flight: the |
| +// response was selected under the old table, but its purge is judged against |
| +// the table it lands in, where no prefix of the front selects the purging |
| +// route any more. |
| +func TestPurgeAcrossRefusedAfterPrefixMoves(t *testing.T) { |
| + for _, tc := range []struct { |
| + name string |
| + move func(*registry.Registry) error |
| + }{ |
| + {"moved to another target", func(reg *registry.Registry) error { |
| + return reg.UpsertPath(registry.RoutePath{Host: selFront, Prefix: "acme", Target: "moved.example.com", Account: selAcct, App: "hub"}) |
| + }}, |
| + {"removed", func(reg *registry.Registry) error { |
| + if err := reg.UpsertPath(registry.RoutePath{Host: selFront, Prefix: "other", Target: "moved.example.com", Account: selAcct, App: "hub"}); err != nil { |
| + return err |
| + } |
| + return reg.DeletePath(selFront, "acme") |
| + }}, |
| + {"target instance replaced", func(reg *registry.Registry) error { |
| + return reg.Upsert(registry.Route{Host: selTarget, Account: selAcct, App: "tenant2", Kind: "instance", Status: "ready", Addr: "127.0.0.1:1"}) |
| + }}, |
| + } { |
| + t.Run(tc.name, func(t *testing.T) { |
| + b := &purgeAcrossBackend{purge: "*"} |
| + s, e := purgeAcrossScene(t, selAcct, b) |
| + wantCase3(t, s) |
| + seed(t, e.cache, selFront, "/acme/events/42", "", "cached") |
| + var moveErr error |
| + b.before = func() { |
| + if moveErr = tc.move(s.reg); moveErr == nil { |
| + moveErr = e.Reload() |
| + } |
| + } |
| + |
| + postAcross(t, e, selFront, "/acme/admin") |
| + |
| + if moveErr != nil { |
| + t.Fatalf("move: %v", moveErr) |
| + } |
| + wantCached(t, e, selFront, "/acme/events/42", "", true, "a route no prefix of the front selects any more purged its pages") |
| + }) |
| + } |
| +} |
| + |
| +// The front's published row no longer names a front route of the claim's |
| +// account and app: the remote map alone grants nothing. The write arrives on |
| +// the target's own host, so the purge check is pinned whatever selection |
| +// does with the front. |
| +func TestPurgeAcrossRefusedWhenFrontNotPublished(t *testing.T) { |
| + for _, tc := range []struct { |
| + name, row string // "" unpublishes the front |
| + }{ |
| + {"control", frontRow(selAcct, "hub")}, |
| + {"another app", frontRow(selAcct, "other")}, |
| + {"another account", frontRow(selOther, "hub")}, |
| + {"not a front", homeRow(selAcct, "hub", false, true)}, |
| + {"unpublished", ""}, |
| + } { |
| + t.Run(tc.name, func(t *testing.T) { |
| + backend := httptest.NewServer(&purgeAcrossBackend{purge: "*"}) |
| + t.Cleanup(backend.Close) |
| + s := newSelScene(t) |
| + s.instance(selTarget, selAcct, "tenant", func(r *registry.Route) { r.Addr = backend.Listener.Addr().String() }) |
| + s.path(selFront, "acme", selTarget, selAcct, "hub") |
| + s.publish("cindy", "unrelated.example.com", frontRow(selAcct, "hub")) |
| + if tc.row != "" { |
| + s.publish("betty", selFront, tc.row) |
| + } |
| + e := s.build() |
| + e.EnableCache(NewCache(DefaultCacheMaxEntries, DefaultCacheMaxBodyBytes)) |
| + seed(t, e.cache, selFront, "/acme/events/42", "", "cached") |
| + |
| + postAcross(t, e, selTarget, "/admin") |
| + |
| + wantCached(t, e, selFront, "/acme/events/42", "", tc.name != "control", "the front's published row decides whether its pages are purgeable here") |
| + }) |
| + } |
| +} |
| + |
| +// The account boundary holds across boxes too. A row naming a target in |
| +// another account (written by an older binary, or by hand) never lets that |
| +// target purge the front's pages: selection refuses it (so the write |
| +// arrives on the target's own host), and so does the purge check. |
| +func TestPurgeAcrossRefusedForOtherAccount(t *testing.T) { |
| + for _, tc := range []struct { |
| + account string |
| + purged bool |
| + }{ |
| + {selAcct, true}, // control: the same write in the claim's account |
| + {selOther, false}, |
| + } { |
| + t.Run(tc.account, func(t *testing.T) { |
| + _, e := purgeAcrossScene(t, tc.account, &purgeAcrossBackend{purge: "*"}) |
| + seed(t, e.cache, selFront, "/acme/events/42", "", "cached") |
| + |
| + postAcross(t, e, selTarget, "/admin") |
| + |
| + wantCached(t, e, selFront, "/acme/events/42", "", !tc.purged, "the account boundary decides") |
| + }) |
| + } |
| +} |
| + |
| +// Scoped to the route a prefix selects: another route of the claim's account |
| +// on this box, which no prefix names, cannot purge the front. |
| +func TestPurgeAcrossRefusedForAnUnselectedRoute(t *testing.T) { |
| + backend := httptest.NewServer(&purgeAcrossBackend{purge: "*"}) |
| + t.Cleanup(backend.Close) |
| + s := newSelScene(t) |
| + s.instance(selTarget, selAcct, "tenant") |
| + s.instance("bystander.example.com", selAcct, "tenant", func(r *registry.Route) { r.Addr = backend.Listener.Addr().String() }) |
| + s.path(selFront, "acme", selTarget, selAcct, "hub") |
| + s.publish("betty", selFront, frontRow(selAcct, "hub")) |
| + e := s.build() |
| + e.EnableCache(NewCache(DefaultCacheMaxEntries, DefaultCacheMaxBodyBytes)) |
| + seed(t, e.cache, selFront, "/acme/events/42", "", "cached") |
| + |
| + postAcross(t, e, "bystander.example.com", "/admin") |
| + |
| + wantCached(t, e, selFront, "/acme/events/42", "", true, "a route no prefix selects purged the front") |
| +} |
| + |
| +// A platform-alias target is its instance (selectedEntry): the instance, |
| +// whose route serves the front's prefix, purges its pages under the front, |
| +// and the alias row itself never acts for it. |
| +func TestPurgeAcrossPlatformAliasTarget(t *testing.T) { |
| + s := newSelScene(t) |
| + const inst, alias = "a.bae.oncarlos.com", "shop.bae.oncarlos.com" |
| + s.instance(inst, selAcct, "shop", func(r *registry.Route) { r.Addr, r.Socket = "", "/s/a.sock" }) |
| + s.instance(alias, selAcct, "shop", func(r *registry.Route) { r.Addr, r.Socket, r.Platform = "", "/s/a.sock", true }) |
| + s.path(selFront, "shop", alias, selAcct, "hub") |
| + s.publish("betty", selFront, frontRow(selAcct, "hub")) |
| + e := s.build() |
| + e.EnableCache(NewCache(DefaultCacheMaxEntries, DefaultCacheMaxBodyBytes)) |
| + got := s.sel(selFront, "/shop/x") |
| + wantLocal(t, got, inst, "shop", alias) |
| + |
| + seed(t, e.cache, selFront, "/shop/x", "", "cached") |
| + h := http.Header{} |
| + h.Set("Carlos-Purge-Host", selFront) |
| + h.Set("Carlos-Purge", "/shop/x") |
| + e.applyPurgeHeader(got.Up.route, selFront, "", h) |
| + wantCached(t, e, selFront, "/shop/x", "", false, "the platform alias's instance could not purge its page under the front") |
| +} |
| + |
| +// The control channel (ownsHost) and the header's execution guard are one |
| +// rule: purgeOwnsIn answers both from one table. |
| +func TestPurgeAcrossOwnsHostFromOneTable(t *testing.T) { |
| + s, e := purgeAcrossScene(t, selAcct, &purgeAcrossBackend{purge: "*"}) |
| + tenant := e.current()[selTarget].route |
| + if !e.ownsHost(tenant, selFront) { |
| + t.Fatal("ownsHost refuses the front host whose prefix selects the tenant here") |
| + } |
| + other := registry.Route{Host: "bystander.example.com", Account: selAcct, App: "tenant"} |
| + if e.ownsHost(other, selFront) { |
| + t.Fatal("ownsHost lets a route no prefix selects own the front host") |
| + } |
| + s.path(selFront, "acme", "moved.example.com", selAcct, "hub") |
| + if err := e.Reload(); err != nil { |
| + t.Fatal(err) |
| + } |
| + if e.ownsHost(tenant, selFront) { |
| + t.Fatal("ownsHost still grants the front after its prefix moved") |
| + } |
| +} |
diff --git a/internal/edge/purge_header.go b/internal/edge/purge_header.go| index d6bcfdea0..cdef5df0a 100644 |
| --- a/internal/edge/purge_header.go |
| +++ b/internal/edge/purge_header.go |
| @@ -143,7 +143,7 @@ const purgeInstanceTarget = "all" |
| // - It redirects EVERY target in the same response, "*" and the cap-degrade |
| // path included, to EVERY accepted host. The override decides WHICH |
| // hosts; §6.1 still decides which entries of each. |
| -// - Resolved against the CURRENT route table at response time (e.current()), |
| +// - Resolved against the CURRENT route table at response time (e.table()), |
| // never a snapshot taken when the proxy was built — a sibling added by the |
| // very reload that rebuilt this proxy must still validate. |
| // - Stripped from the response UNCONDITIONALLY, in the same place |
| @@ -309,9 +309,9 @@ func scrubInboundPurgeHeader(pr *httputil.ProxyRequest) { |
| // time the response comes back. |
| // |
| // The OVERRIDE side is deliberately the other way round: the named host is |
| -// looked up in e.current() at response time (see resolvePurgeHosts), because |
| +// looked up in e.table() at response time (see resolvePurgeHosts), because |
| // the route table is live and a sibling added since this proxy was built must |
| -// still validate. e.current() is an atomic pointer load, so reading it from |
| +// still validate. e.table() is an atomic pointer load, so reading it from |
| // this hook needs no lock and cannot tear against a concurrent Reload. |
| func (e *Edge) purgeModifier(rt registry.Route) func(*http.Response) error { |
| return func(resp *http.Response) error { |
| @@ -367,17 +367,17 @@ func (e *Edge) applyPurgeHeader(rt registry.Route, asked, public string, h http. |
| } |
| // ONE snapshot of the route table for the whole declaration: the hosts |
| // it reaches, the instance each acts for, and the hosts "all" expands to |
| - // are all judged against the same table. |
| - cur := e.current() |
| - owned, ok := e.resolvePurgeOwnersIn(cur, rt, public, declared) |
| + // are all judged against the same table, its remote path map included. |
| + t := e.purgeTable() |
| + owned, ok := e.resolvePurgeOwnersIn(t, rt, public, declared) |
| if !ok { |
| return |
| } |
| pages := owned |
| if len(declared) == 0 { |
| - pages = pageHostsFor(cur, rt, asked) |
| + pages = pageHostsFor(t.hosts, rt, asked) |
| } |
| - e.applyPurgeTargets(cur, rt.Host, owned, pages, joined) |
| + e.applyPurgeTargets(t, rt.Host, owned, pages, joined) |
| } |
| // purgeHost is one host a purge declaration reaches, with the instance it |
| @@ -430,7 +430,7 @@ func pageHostsFor(m map[string]upstream, rt registry.Route, asked string) []purg |
| // |
| // declared is every Carlos-Purge-Host LINE, already removed from the response. |
| func (e *Edge) resolvePurgeHosts(rt registry.Route, declared []string) ([]string, bool) { |
| - owned, ok := e.resolvePurgeOwnersIn(e.current(), rt, "", declared) |
| + owned, ok := e.resolvePurgeOwnersIn(e.purgeTable(), rt, "", declared) |
| if !ok { |
| return nil, false |
| } |
| @@ -441,11 +441,14 @@ func (e *Edge) resolvePurgeHosts(rt registry.Route, declared []string) ([]string |
| return hosts, true |
| } |
| -// resolvePurgeOwnersIn is resolvePurgeHosts against the snapshot cur, with |
| +// resolvePurgeOwnersIn is resolvePurgeHosts against the snapshot t, with |
| // the instance each accepted host acts for (purgeHost). public, when not |
| // empty, is the public host a linked request was served under |
| -// (ServeLinked): naming it is naming rt's own host. |
| -func (e *Edge) resolvePurgeOwnersIn(cur map[string]upstream, rt registry.Route, public string, declared []string) ([]purgeHost, bool) { |
| +// (ServeLinked): naming it is naming rt's own host. A host t.hosts does not |
| +// serve is accepted, for rt, only as a front whose prefix selects rt here |
| +// (frontPathOwns); any other is skipped as unserved. |
| +func (e *Edge) resolvePurgeOwnersIn(t *routeTable, rt registry.Route, public string, declared []string) ([]purgeHost, bool) { |
| + cur := t.hosts |
| if len(declared) == 0 { |
| return []purgeHost{{rt.Host, rt}}, true |
| } |
| @@ -496,10 +499,10 @@ func (e *Edge) resolvePurgeOwnersIn(cur map[string]upstream, rt registry.Route, |
| return nil, false |
| } |
| } |
| - // cur is e.current(), not a captured map: the route table is live, and a |
| - // sibling added after this proxy was built must still validate. ONE |
| - // snapshot for the whole list, so every entry is judged against the same |
| - // table. |
| + // cur is t.hosts, from e.table(), not a captured map: the route table is |
| + // live, and a sibling added after this proxy was built must still |
| + // validate. ONE snapshot for the whole list, its remote path map |
| + // included, so every entry is judged against the same table. |
| hosts := make([]purgeHost, 0, len(vals)) |
| var unserved []string |
| servingChecked := false |
| @@ -527,6 +530,15 @@ func (e *Edge) resolvePurgeOwnersIn(cur map[string]upstream, rt registry.Route, |
| // one judged below. |
| up, ok := lookupIn(cur, host) |
| if !ok { |
| + // A front host this box does not serve (path routes across |
| + // boxes, case 3): rt's pages under it are rt's own while a |
| + // prefix of it selects rt here, judged in t. Anything else, |
| + // another account's front included, is skipped as unserved, |
| + // exactly as before: this box holds no route for it to judge. |
| + if rm, mapped := t.remote[host]; mapped && e.frontPathOwns(cur, rm, rt, host) { |
| + hosts = append(hosts, purgeHost{host, rt}) |
| + continue |
| + } |
| unserved = append(unserved, host) |
| continue |
| } |
| @@ -727,12 +739,12 @@ func validPurgeOverrideHost(h string) bool { |
| // both, wiping a host nobody asked about while leaving the stale pages the app |
| // was trying to invalidate. |
| // |
| -// Every LRU purge is of a host its owner still owns in cur (purgeOwns, ledger |
| +// Every LRU purge is of a host its owner still owns in t (purgeOwnsIn, ledger |
| // R10): a response whose hostname moved to another instance while it was in |
| // flight purges nothing of that instance's from the LRU, its primary host |
| // included. Page clears need no such test: they are pinned to the owner's |
| // own scope (clearPages). |
| -func (e *Edge) applyPurgeTargets(cur map[string]upstream, from string, hosts, pageHosts []purgeHost, joined string) { |
| +func (e *Edge) applyPurgeTargets(t *routeTable, from string, hosts, pageHosts []purgeHost, joined string) { |
| if len(hosts) == 0 { |
| return |
| } |
| @@ -744,7 +756,7 @@ func (e *Edge) applyPurgeTargets(cur map[string]upstream, from string, hosts, pa |
| // lru is the hosts whose LRU entries this declaration may purge. |
| lru := make([]purgeHost, 0, len(hosts)) |
| for _, p := range hosts { |
| - if purgeOwns(cur, p.owner, p.host) { |
| + if e.purgeOwnsIn(t, p.owner, p.host) { |
| lru = append(lru, p) |
| } |
| } |
| @@ -779,17 +791,17 @@ func (e *Edge) applyPurgeTargets(cur map[string]upstream, from string, hosts, pa |
| // would refuse a target that is not a path. |
| // |
| // Each host is purged as "*" would purge it, and the expansion is to the |
| - // hosts cur serves as that host's OWNER (purgeHost), never to whatever |
| + // hosts t serves as that host's OWNER (purgeHost), never to whatever |
| // instance holds the hostname now: a response still in flight when its |
| // hostname moved to another instance cannot reach that instance's |
| // aliases or pages. |
| if slices.Contains(targets, purgeInstanceTarget) { |
| cached := e.cache.cachedHosts() |
| for _, p := range hosts { |
| - if purgeOwns(cur, p.owner, p.host) { |
| + if e.purgeOwnsIn(t, p.owner, p.host) { |
| e.cache.PurgeHost(p.host) |
| } |
| - for _, h := range instanceHostsIn(cur, p.owner, cached) { |
| + for _, h := range instanceHostsIn(t.hosts, p.owner, cached) { |
| if h != p.host { |
| e.cache.PurgeHost(h) |
| } |
| @@ -854,19 +866,29 @@ func (e *Edge) applyPurgeTargets(cur map[string]upstream, from string, hosts, pa |
| } |
| } |
| -// purgeOwns reports whether a purge acting for owner may touch the LRU |
| -// entries cached under host, judged in the one routing snapshot m (ledger |
| -// R10, purge authority): m resolves host (lookupIn) to owner's instance, or |
| -// host path-routes to owner while m still serves owner's primary host as |
| -// owner's instance (the path-routing exception: the pages owner serves |
| -// under host are its own). A hostname that has moved to another instance is |
| -// that instance's, so a purge still on its way from the old one, by a |
| -// response or a control listener built before the move, leaves its LRU |
| +// purgeOwnsIn reports whether a purge acting for owner may touch the LRU |
| +// entries cached under host, judged in the one routing snapshot t (ledger |
| +// R10, purge authority): t.hosts resolves host (lookupIn) to owner's |
| +// instance, or host path-routes to owner while t.hosts still serves owner's |
| +// primary host as owner's instance (the path-routing exception: the pages |
| +// owner serves under host are its own). A hostname that has moved to another |
| +// instance is that instance's, so a purge still on its way from the old one, |
| +// by a response or a control listener built before the move, leaves its LRU |
| // entries alone. |
| -func purgeOwns(m map[string]upstream, owner registry.Route, host string) bool { |
| +// |
| +// A host t.hosts does not resolve at all is owner's only as a front another |
| +// box serves whose prefix selects owner here (frontPathOwns, spec §7.2). |
| +// Every host this box resolves, through a catch-all included, is judged as |
| +// before path routes crossed boxes. |
| +func (e *Edge) purgeOwnsIn(t *routeTable, owner registry.Route, host string) bool { |
| + if t == nil { |
| + return false |
| + } |
| + m := t.hosts |
| up, ok := lookupIn(m, host) |
| if !ok { |
| - return false |
| + rm, mapped := t.remote[host] |
| + return mapped && e.frontPathOwns(m, rm, owner, host) |
| } |
| if sameInstance(up.route, owner) { |
| return true |
| @@ -878,6 +900,60 @@ func purgeOwns(m map[string]upstream, owner registry.Route, host string) bool { |
| return ok && sameInstance(primary.route, owner) |
| } |
| +// frontPathOwns reports whether owner's pages under host, a front this box |
| +// does not serve, are owner's to purge (path routes across boxes, case 3, |
| +// spec §7.2), judged in one snapshot: hosts and rm, host's remote path map, |
| +// come from the same routeTable. Each of these must hold: |
| +// |
| +// - owner is in the claim's account (rm.account, as release.RouteAccount |
| +// names a route's account); |
| +// - the home map's exact row for host is a front route of the claim's |
| +// account and app, as selection requires before it reads rm |
| +// (selectForUnservedFront); |
| +// - a prefix of rm selects owner here: its target is one unservedFrontTarget |
| +// answers with owner's instance, which is hosts[owner.Host] (a |
| +// platform-alias target is its instance). So owner is still the instance |
| +// served here, and a prefix that moved, or an owner replaced while its |
| +// response was in flight, grants nothing. |
| +// |
| +// Selection and purge ask the same question of the same functions, so a |
| +// route purges a front's pages exactly while a request for one of its |
| +// prefixes would be served by that route here. |
| +// |
| +// A linear scan of rm's prefixes, as pathRoutesTo is, for the same reason: |
| +// it runs only for a purge naming such a host, and a claim's map is capped |
| +// (domains.MaxPathsPerClaim). The cheap name test runs first, so only a |
| +// prefix naming owner's own host or a platform alias of it is judged. |
| +func (e *Edge) frontPathOwns(hosts map[string]upstream, rm remotePathMap, owner registry.Route, host string) bool { |
| + if owner.Host == "" || release.RouteAccount(owner) != rm.account { |
| + return false |
| + } |
| + fi, ok := e.homes.Load().exactPathRoute(host) |
| + if !ok || !fi.Front || fi.Account != rm.account || fi.App != rm.app { |
| + return false |
| + } |
| + for _, target := range rm.prefixes { |
| + if target != owner.Host { |
| + if tu, here := hosts[target]; !here || !tu.platformAlias || tu.route.Host != owner.Host { |
| + continue |
| + } |
| + } |
| + if tu, ok := unservedFrontTarget(hosts, target, rm.account); ok && sameInstance(tu.route, owner) { |
| + return true |
| + } |
| + } |
| + return false |
| +} |
| + |
| +// purgeTable is the published routing snapshot, or an empty one before the |
| +// first Reload, so a purge judged then finds nothing to own. |
| +func (e *Edge) purgeTable() *routeTable { |
| + if t := e.table(); t != nil { |
| + return t |
| + } |
| + return &routeTable{} |
| +} |
| + |
| // logPurgeIssue rate-limits one class of purge complaint for one host, using |
| // the same first-then-every-300th shape as the registry watcher in edge.go. |
| // |