carlos / platform Public

console: the edge-fold refresh a page waits for carries the page's context

foldedEdgeIPs refreshed the fold on the caller's goroutine under
context.Background(), so its S3 listings counted as background work
while the page waited for them. dnsTargets, edgeIPs and foldedEdgeIPs
now take the caller's context; the refresh runs under
context.WithoutCancel of it with the same edgeFoldTimeout, so the
shared cache is still never cut short by a caller that gives up.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 52379423640ff7ae08f4748291011f473581d012 parent 5f49f01
6 files changed, +61 −24
  • internal/console/apidomains.go +3 −3
  • internal/console/appsettings.go +1 −1
  • internal/console/domaincatchall.go +1 −1
  • internal/console/domainstate.go +1 −1
  • internal/console/edgefold_test.go +38 −8
  • internal/console/server.go +17 −10
diff --git a/internal/console/apidomains.go b/internal/console/apidomains.go
index a44711821..44a2e7be3 100644
--- a/internal/console/apidomains.go
+++ b/internal/console/apidomains.go
@@ -1128,7 +1128,7 @@ func (s *Server) handleAPIDomainList(w http.ResponseWriter, r *http.Request) {
warm := warmRecords(ctx, s.cfg.Releases)
// Read ONCE per call, not per claim: dnsTargets folds the deployment's
// edge set, and a per-claim read would refold it for every hostname.
- targets := s.dnsTargets()
+ targets := s.dnsTargets(ctx)
// This app's claims, host-sorted BEFORE the loop below — not just for
// the answer's own stable order (the Store.List contract makes no
// promise, and a terminal listing that shuffles between calls reads as
@@ -1276,7 +1276,7 @@ func (s *Server) handleAPIDomainGet(w http.ResponseWriter, r *http.Request) {
}
now := time.Now()
items := s.catchallListItems(ctx, acct, app, *mine, claimed, aliasReadings(ctx, s.cfg.Releases, now),
- warmRecords(ctx, s.cfg.Releases), s.dnsTargets(), now)
+ warmRecords(ctx, s.cfg.Releases), s.dnsTargets(ctx), now)
writeJSON(w, http.StatusOK, map[string]any{
"domain": items[0], "derived": items[1:],
"deployment": s.domainCapability(ctx, acct.Sqid, app), "paths": map[string]string{},
@@ -1312,7 +1312,7 @@ func (s *Server) handleAPIDomainGet(w http.ResponseWriter, r *http.Request) {
now := time.Now()
writeJSON(w, http.StatusOK, map[string]any{
"domain": s.domainItem(ctx, *c, aliasReadings(ctx, s.cfg.Releases, now),
- warmRecord(ctx, s.cfg.Releases, c.Host), s.dnsTargets(), now,
+ warmRecord(ctx, s.cfg.Releases, c.Host), s.dnsTargets(ctx), now,
c.Wildcard && s.delegationZone != ""),
"deployment": s.domainCapability(ctx, acct.Sqid, app),
// The prefix map as PUBLISHED, and this is the only surface that
diff --git a/internal/console/appsettings.go b/internal/console/appsettings.go
index 40556867c..742da7064 100644
--- a/internal/console/appsettings.go
+++ b/internal/console/appsettings.go
@@ -733,7 +733,7 @@ func (s *Server) settingsDomains(ctx context.Context, acct *Account, app string,
}
continue
}
- dv := claimDomainView(c.Host, s.dnsTargets())
+ dv := claimDomainView(c.Host, s.dnsTargets(ctx))
dv.State, dv.Reason, dv.Detail = state(c)
if c.Pending {
// The proof comes first: until it is published nothing else on
diff --git a/internal/console/domaincatchall.go b/internal/console/domaincatchall.go
index e6a97a7c9..0b37f28a5 100644
--- a/internal/console/domaincatchall.go
+++ b/internal/console/domaincatchall.go
@@ -349,7 +349,7 @@ func (s *Server) catchallListItems(ctx context.Context, acct *Account, app strin
// catch-all's own row (detachable, by typing "*.<parent>") and one row per
// derived name, which is not.
func (s *Server) catchallDomainViews(ctx context.Context, acct *Account, app string, c domains.Claim, claimed map[string]bool, readings map[string]status.AliasReading, warm map[string]*certwarm.Record, now time.Time) []DomainView {
- items := s.catchallListItems(ctx, acct, app, c, claimed, readings, warm, s.dnsTargets(), now)
+ items := s.catchallListItems(ctx, acct, app, c, claimed, readings, warm, s.dnsTargets(ctx), now)
out := make([]DomainView, 0, len(items))
for _, it := range items {
dv := DomainView{Domain: it.Host, Guidance: it.Guidance, State: it.State, Reason: it.Reason, Detail: it.Detail}
diff --git a/internal/console/domainstate.go b/internal/console/domainstate.go
index ba77300a9..11f89f532 100644
--- a/internal/console/domainstate.go
+++ b/internal/console/domainstate.go
@@ -268,7 +268,7 @@ type deploymentCapability struct {
}
func (s *Server) domainCapability(ctx context.Context, acct, app string) deploymentCapability {
- t := s.dnsTargets()
+ t := s.dnsTargets(ctx)
features, setAt := s.appFeatures(ctx, acct, app)
return deploymentCapability{
ManagedDomains: true,
diff --git a/internal/console/edgefold_test.go b/internal/console/edgefold_test.go
index 04c153bcb..e4f92b7a3 100644
--- a/internal/console/edgefold_test.go
+++ b/internal/console/edgefold_test.go
@@ -20,7 +20,7 @@ func TestDNSTargetsUnionsTheConfiguredListWithTheFold(t *testing.T) {
cfg: Config{EdgeIPs: []string{"203.0.113.7"}},
foldEdgeIPs: func(context.Context) []string { return []string{"198.51.100.9", "203.0.113.7"} },
}
- got := s.dnsTargets()
+ got := s.dnsTargets(context.Background())
if !reflect.DeepEqual(got.EdgeIPs, []string{"203.0.113.7", "198.51.100.9"}) {
t.Fatalf("EdgeIPs = %v, want the configured list first, then the fold's extras, deduped", got.EdgeIPs)
}
@@ -37,7 +37,7 @@ func TestEdgeFoldIsCachedForTheTTL(t *testing.T) {
return []string{"198.51.100.9"}
}}
for i := 0; i < 5; i++ {
- s.dnsTargets()
+ s.dnsTargets(context.Background())
}
if folds != 1 {
t.Fatalf("folded %d times inside the TTL, want 1", folds)
@@ -48,7 +48,7 @@ func TestEdgeFoldIsCachedForTheTTL(t *testing.T) {
s.edgeFold.mu.Lock()
s.edgeFold.at = time.Now().Add(-edgeFoldTTL - time.Second)
s.edgeFold.mu.Unlock()
- if got := s.dnsTargets(); folds != 2 || len(got.EdgeIPs) != 1 {
+ if got := s.dnsTargets(context.Background()); folds != 2 || len(got.EdgeIPs) != 1 {
t.Fatalf("after the TTL: folds = %d, EdgeIPs = %v, want a refold", folds, got.EdgeIPs)
}
}
@@ -68,13 +68,13 @@ func TestEdgeFoldKeepsTheLastAnswerWhenAFoldFails(t *testing.T) {
}
return nil // the store is down
}}
- if got := s.dnsTargets(); len(got.EdgeIPs) != 1 {
+ if got := s.dnsTargets(context.Background()); len(got.EdgeIPs) != 1 {
t.Fatalf("first fold: EdgeIPs = %v", got.EdgeIPs)
}
s.edgeFold.mu.Lock()
s.edgeFold.at = time.Now().Add(-edgeFoldTTL - time.Second)
s.edgeFold.mu.Unlock()
- got := s.dnsTargets()
+ got := s.dnsTargets(context.Background())
if folds != 2 {
t.Fatalf("folds = %d, want the stale entry to have been retried", folds)
}
@@ -84,7 +84,7 @@ func TestEdgeFoldKeepsTheLastAnswerWhenAFoldFails(t *testing.T) {
// And the failed attempt still moved the clock: a console whose bucket is
// down must not refold on every request of every listing.
before := folds
- s.dnsTargets()
+ s.dnsTargets(context.Background())
if folds != before {
t.Fatalf("folds = %d, want the failed attempt to still hold off for the TTL", folds)
}
@@ -103,7 +103,7 @@ func TestRealEdgeFoldReadsTheDeploymentsEdgesFiles(t *testing.T) {
t.Fatal(err)
}
s := &Server{cfg: Config{EdgeIPs: []string{"203.0.113.7"}, Releases: store}}
- if got := s.dnsTargets(); !reflect.DeepEqual(got.EdgeIPs, []string{"203.0.113.7", "198.51.100.9"}) {
+ if got := s.dnsTargets(context.Background()); !reflect.DeepEqual(got.EdgeIPs, []string{"203.0.113.7", "198.51.100.9"}) {
t.Fatalf("EdgeIPs = %v, want the configured address plus the folded one", got.EdgeIPs)
}
}
@@ -113,7 +113,37 @@ func TestRealEdgeFoldReadsTheDeploymentsEdgesFiles(t *testing.T) {
// emptying the guidance.
func TestRealEdgeFoldWithNoStoreDegradesToTheConfiguredList(t *testing.T) {
s := &Server{cfg: Config{EdgeIPs: []string{"203.0.113.7"}}}
- if got := s.dnsTargets(); !reflect.DeepEqual(got.EdgeIPs, []string{"203.0.113.7"}) {
+ if got := s.dnsTargets(context.Background()); !reflect.DeepEqual(got.EdgeIPs, []string{"203.0.113.7"}) {
t.Fatalf("EdgeIPs = %v, want the configured list", got.EdgeIPs)
}
}
+
+type edgeFoldCtxKey struct{}
+
+// A page waits for the fold refresh it triggers, so the refresh carries
+// the page's context values (the perf middleware attributes its S3 calls
+// by them) but not its cancellation: the fold fills a shared cache, and a
+// caller that has given up must not cut it short.
+func TestEdgeFoldCarriesTheCallersValuesNotItsCancellation(t *testing.T) {
+ var gotValue any
+ var gotErr error
+ var gotDeadline bool
+ s := &Server{foldEdgeIPs: func(ctx context.Context) []string {
+ gotValue, gotErr = ctx.Value(edgeFoldCtxKey{}), ctx.Err()
+ dl, ok := ctx.Deadline()
+ gotDeadline = ok && time.Until(dl) <= edgeFoldTimeout
+ return []string{"198.51.100.9"}
+ }}
+ ctx, cancel := context.WithCancel(context.WithValue(context.Background(), edgeFoldCtxKey{}, "page"))
+ cancel()
+ s.dnsTargets(ctx)
+ if gotValue != "page" {
+ t.Errorf("fold saw value %v, want the caller's", gotValue)
+ }
+ if gotErr != nil {
+ t.Errorf("fold context err = %v, want the caller's cancellation dropped", gotErr)
+ }
+ if !gotDeadline {
+ t.Errorf("fold context has no edgeFoldTimeout deadline")
+ }
+}
diff --git a/internal/console/server.go b/internal/console/server.go
index dd12d3b8f..59d0c63da 100644
--- a/internal/console/server.go
+++ b/internal/console/server.go
@@ -1261,9 +1261,10 @@ func (s *Server) chrome(p *page) {
// built from (view.go's domainGuidance): the deployment's edge addresses and
// its steering hostname, read from ONE place so the app page, the Settings
// page and the /domains API can never disagree about where a member should
-// point DNS.
-func (s *Server) dnsTargets() DNSTargets {
- return DNSTargets{EdgeIPs: s.edgeIPs(), SteeringHost: s.steeringHost}
+// point DNS. ctx is the caller's request: a fold refresh it waits for is
+// counted against that request (see foldedEdgeIPs).
+func (s *Server) dnsTargets(ctx context.Context) DNSTargets {
+ return DNSTargets{EdgeIPs: s.edgeIPs(ctx), SteeringHost: s.steeringHost}
}
// edgeIPs is the deployment's edge addresses as this console will tell a
@@ -1276,9 +1277,9 @@ func (s *Server) dnsTargets() DNSTargets {
// edges appear even though this console's env var predates them, which is
// the whole point: the same set the domains reconcile pass now judges
// "pointing" against, so guidance and verdict cannot disagree.
-func (s *Server) edgeIPs() []string {
+func (s *Server) edgeIPs(ctx context.Context) []string {
out := append([]string(nil), s.cfg.EdgeIPs...)
- for _, ip := range s.foldedEdgeIPs() {
+ for _, ip := range s.foldedEdgeIPs(ctx) {
if ip != "" && !slices.Contains(out, ip) {
out = append(out, ip)
}
@@ -1295,7 +1296,13 @@ func (s *Server) edgeIPs() []string {
// silently narrow the guidance every member is shown. The timestamp still
// moves, so a persistent outage costs one bounded attempt per TTL rather than
// one per request.
-func (s *Server) foldedEdgeIPs() []string {
+//
+// The refresh runs on the caller's goroutine, which waits for it, so it
+// carries ctx's values (the perf middleware counts its S3 requests against
+// that request) but not its cancellation or deadline: the fold fills a
+// cache every request shares, and a caller that gives up must not cut it
+// short. edgeFoldTimeout bounds it, as before.
+func (s *Server) foldedEdgeIPs(ctx context.Context) []string {
c := &s.edgeFold
c.mu.Lock()
defer c.mu.Unlock()
@@ -1306,9 +1313,9 @@ func (s *Server) foldedEdgeIPs() []string {
if fold == nil {
fold = s.realFoldEdgeIPs
}
- ctx, cancel := context.WithTimeout(context.Background(), edgeFoldTimeout)
+ fctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), edgeFoldTimeout)
defer cancel()
- ips := fold(ctx)
+ ips := fold(fctx)
c.at = time.Now()
if len(ips) > 0 {
c.ips = ips
@@ -3336,7 +3343,7 @@ func (s *Server) handleAccountHome(w http.ResponseWriter, r *http.Request) {
return
}
if len(apps) > 0 {
- views, err := Assemble(r.Context(), s.cfg.Registry, s.cfg.Releases, s.cfg.ReleasePub, acct.Sqid, apps, s.dnsTargets())
+ views, err := Assemble(r.Context(), s.cfg.Registry, s.cfg.Releases, s.cfg.ReleasePub, acct.Sqid, apps, s.dnsTargets(r.Context()))
if err != nil {
log.Printf("console: assemble: %v", err)
s.renderMessage(w, r, http.StatusInternalServerError, "Error", "Could not read platform state. Please try again.")
@@ -3499,7 +3506,7 @@ func (s *Server) appPage(w http.ResponseWriter, r *http.Request, tab string) (pa
// bucket hiccup there is deliberately non-fatal, and this is where it gets
// another chance. Best-effort — a page view never fails on it.
s.ensureAccountManifest(r.Context(), acct)
- views, err := Assemble(r.Context(), s.cfg.Registry, s.cfg.Releases, s.cfg.ReleasePub, acct.Sqid, []string{app}, s.dnsTargets())
+ views, err := Assemble(r.Context(), s.cfg.Registry, s.cfg.Releases, s.cfg.ReleasePub, acct.Sqid, []string{app}, s.dnsTargets(r.Context()))
if err != nil || len(views) != 1 {
log.Printf("console: assemble %s: %v", app, err)
s.renderMessage(w, r, http.StatusInternalServerError, "Error", "Could not read platform state. Please try again.")