host, edge: a turned-off host gets a plain page (plans 4d)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
6 files changed,
+356
−1
internal/edge/edge.go+7 −0internal/edge/plandisabled.go+88 −0internal/edge/plandisabled_test.go+112 −0internal/host/agent.go+3 −0internal/host/plans_disabled_test.go+101 −0internal/host/plans_pass.go+45 −1
diff --git a/internal/edge/edge.go b/internal/edge/edge.go| index d138d7e2a..0eb588d36 100644 |
| --- a/internal/edge/edge.go |
| +++ b/internal/edge/edge.go |
| @@ -329,6 +329,8 @@ type Edge struct { |
| // account's open upgraded connections, closable when it is throttled. |
| planThrottle atomic.Pointer[func(registry.Route) (plans.Throttle, bool)] |
| planGoverned atomic.Pointer[func(registry.Route) bool] |
| + // planDisabled is the turned-off hook (plans 4d, plandisabled.go). |
| + planDisabled atomic.Pointer[func(registry.Route, string) bool] |
| planLimiter *ratelimit.Limiter |
| upgradesMu sync.Mutex |
| upgrades map[string]map[*int]context.CancelFunc |
| @@ -1701,6 +1703,11 @@ func (e *Edge) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| if !e.importGateAdmit(w, r, host, up.route) { |
| return |
| } |
| + // A route or host a lapse turned off (plandisabled.go): the same place, |
| + // so it is not metered, gated, cached or woken either. |
| + if !e.planDisabledAdmit(w, r, host, up.route) { |
| + return |
| + } |
| // Observe every route this box serves except the platform's own status |
| // host (not billable, and not a member's app) — wrapping w here, once, |
diff --git a/internal/edge/plandisabled.go b/internal/edge/plandisabled.go| new file mode 100644 |
| index 000000000..acf65f3dc |
| --- /dev/null |
| +++ b/internal/edge/plandisabled.go |
| @@ -0,0 +1,88 @@ |
| +package edge |
| + |
| +import ( |
| + "bytes" |
| + "html/template" |
| + "net/http" |
| + |
| + "github.com/carlosframework/platform/internal/registry" |
| +) |
| + |
| +// The disabled page (plans 4d): what visitors get for something a lapsed |
| +// account's plan no longer covers. It says nothing about plans or payment, |
| +// so an owner's billing is not published (Call 1); the details are the |
| +// account's, in the console. A 503 with no-store and noindex, no |
| +// Retry-After: nothing will change by waiting. It never echoes the host or |
| +// anything else from the request. |
| + |
| +// The page's words. Kept together so a copy change is one place. |
| +const ( |
| + disabledTitle = "Not available" |
| + disabledHeading = "This site is not available right now" |
| + disabledBody = "If it is yours, sign in to your console to see why." |
| +) |
| + |
| +var disabledTemplate = template.Must(template.New("disabled").Parse(`<!doctype html> |
| +<html lang="en"> |
| +<head> |
| +<meta charset="utf-8"> |
| +<meta name="viewport" content="width=device-width, initial-scale=1"> |
| +<meta name="robots" content="noindex"> |
| +<title>{{.Title}}</title> |
| +<style> |
| +body{font:16px/1.5 system-ui,sans-serif;max-width:32rem;margin:20vh auto 0;padding:0 1.5rem;color:#222} |
| +h1{font-size:1.25rem;font-weight:600;margin:0 0 .5rem} |
| +p{margin:0;color:#555} |
| +</style> |
| +</head> |
| +<body> |
| +<h1>{{.Heading}}</h1> |
| +<p>{{.Body}}</p> |
| +</body> |
| +</html> |
| +`)) |
| + |
| +// disabledPage is rendered once: its only inputs are constants. |
| +var disabledPage = func() []byte { |
| + var buf bytes.Buffer |
| + if err := disabledTemplate.Execute(&buf, map[string]string{ |
| + "Title": disabledTitle, "Heading": disabledHeading, "Body": disabledBody, |
| + }); err != nil { |
| + panic(err) // fixed at build time; caught by the package's tests |
| + } |
| + return buf.Bytes() |
| +}() |
| + |
| +// SetPlanDisabledFor installs the turned-off hook (plans 4d): whether a lapse |
| +// turned off what a request reaches, given the route it resolved to and the |
| +// request's host (internal/host's planDisabledFor). nil, the default and |
| +// every box without a plan catalog, turns nothing off. Safe to call while |
| +// serving. |
| +func (e *Edge) SetPlanDisabledFor(f func(rt registry.Route, host string) bool) { |
| + if f == nil { |
| + e.planDisabled.Store(nil) |
| + return |
| + } |
| + e.planDisabled.Store(&f) |
| +} |
| + |
| +// planDisabledAdmit answers a turned-off route or host with the disabled |
| +// page and reports false; true lets the request on. ServeHTTP calls it |
| +// after the import gate, before the meter, the password gate, the cache and |
| +// the activator, so a turned-off thing is not served, billed or woken. The |
| +// platform's status host is never turned off. |
| +func (e *Edge) planDisabledAdmit(w http.ResponseWriter, r *http.Request, host string, rt registry.Route) bool { |
| + f := e.planDisabled.Load() |
| + if f == nil || rt.Kind == "status" || !(*f)(rt, host) { |
| + return true |
| + } |
| + e.logThrottled("plan-disabled "+host, "plan: answered "+host+" with the disabled page (turned off after a lapse)") |
| + h := w.Header() |
| + h.Set("Content-Type", "text/html; charset=utf-8") |
| + h.Set("Cache-Control", "no-store") |
| + w.WriteHeader(http.StatusServiceUnavailable) |
| + if r.Method != http.MethodHead { |
| + w.Write(disabledPage) |
| + } |
| + return false |
| +} |
diff --git a/internal/edge/plandisabled_test.go b/internal/edge/plandisabled_test.go| new file mode 100644 |
| index 000000000..697d8a4dc |
| --- /dev/null |
| +++ b/internal/edge/plandisabled_test.go |
| @@ -0,0 +1,112 @@ |
| +package edge |
| + |
| +import ( |
| + "net/http" |
| + "net/http/httptest" |
| + "strings" |
| + "testing" |
| + |
| + "github.com/carlosframework/platform/internal/registry" |
| +) |
| + |
| +// The disabled page (plans 4d). Neutral fixtures: app shop, an instance at |
| +// shop.apps.example with the custom alias shop.example.com. |
| +const ( |
| + disabledHost = "shop.apps.example" |
| + disabledAlias = "shop.example.com" |
| +) |
| + |
| +type disabledScene struct { |
| + e *Edge |
| + act *fakeActivator |
| + backend *gateBackend |
| +} |
| + |
| +func newDisabledScene(t *testing.T, listed ...string) *disabledScene { |
| + t.Helper() |
| + srv, backend := newGateBackend(t, "app", "") |
| + reg := openReg(t) |
| + if err := reg.Upsert(registry.Route{Host: disabledHost, Kind: "instance", Addr: srv.Listener.Addr().String(), |
| + Status: "ready", App: "shop", Account: "bab", Hibernate: true}); err != nil { |
| + t.Fatal(err) |
| + } |
| + upsertAlias(t, reg, disabledAlias, disabledHost) |
| + act := &fakeActivator{} |
| + e, err := New(reg, act) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + t.Cleanup(e.probers.Stop) |
| + if listed != nil { |
| + set := map[string]bool{} |
| + for _, h := range listed { |
| + set[h] = true |
| + } |
| + e.SetPlanDisabledFor(func(rt registry.Route, host string) bool { return set[rt.Host] || set[host] }) |
| + } |
| + return &disabledScene{e: e, act: act, backend: backend} |
| +} |
| + |
| +func wantDisabledPage(t *testing.T, rec *httptest.ResponseRecorder, what string) { |
| + t.Helper() |
| + if rec.Code != http.StatusServiceUnavailable || rec.Header().Get("Cache-Control") != "no-store" || |
| + rec.Header().Get("Retry-After") != "" || rec.Header().Get("Content-Type") != "text/html; charset=utf-8" { |
| + t.Fatalf("%s: %d cache %q retry %q type %q, want the disabled page's 503", what, rec.Code, |
| + rec.Header().Get("Cache-Control"), rec.Header().Get("Retry-After"), rec.Header().Get("Content-Type")) |
| + } |
| + body := rec.Body.String() |
| + if !strings.Contains(body, disabledHeading) || !strings.Contains(body, `<meta name="robots" content="noindex">`) { |
| + t.Fatalf("%s: body lacks the heading or noindex:\n%s", what, body) |
| + } |
| +} |
| + |
| +func TestDisabledHostGetsThePage(t *testing.T) { |
| + t.Parallel() |
| + s := newDisabledScene(t, disabledHost) |
| + wantDisabledPage(t, gateDo(s.e, http.MethodGet, disabledHost, "/", ""), "GET") |
| + if n, _ := s.backend.seen(); n != 0 || s.act.served != 0 { |
| + t.Fatalf("upstream saw %d, activator served %d; want neither", n, s.act.served) |
| + } |
| + head := gateDo(s.e, http.MethodHead, disabledHost, "/", "") |
| + if head.Code != http.StatusServiceUnavailable || head.Body.Len() != 0 { |
| + t.Fatalf("HEAD: %d with %d body bytes", head.Code, head.Body.Len()) |
| + } |
| +} |
| + |
| +func TestDisabledAliasOnly(t *testing.T) { |
| + t.Parallel() |
| + s := newDisabledScene(t, disabledAlias) |
| + wantDisabledPage(t, gateDo(s.e, http.MethodGet, disabledAlias, "/", ""), "the alias") |
| + rec := gateDo(s.e, http.MethodGet, disabledHost, "/", "") |
| + if rec.Code != http.StatusOK || rec.Body.String() != "app" { |
| + t.Fatalf("the platform host: %d %q, want it served", rec.Code, rec.Body.String()) |
| + } |
| +} |
| + |
| +func TestDisabledPageEchoesNothing(t *testing.T) { |
| + t.Parallel() |
| + s := newDisabledScene(t, disabledHost) |
| + plain := gateDo(s.e, http.MethodGet, disabledHost, "/", "").Body.String() |
| + req := httptest.NewRequest(http.MethodGet, "http://"+disabledHost+"/%3Cscript%3Ealert(1)%3C/script%3E?x=%22evil%22", nil) |
| + req.Host = disabledHost |
| + req.Header.Set("X-Evil", "<b>evil</b>") |
| + req.Header.Set("Referer", "https://evil.example/<i>") |
| + rec := httptest.NewRecorder() |
| + s.e.ServeHTTP(rec, req) |
| + if rec.Body.String() != plain || plain != string(disabledPage) { |
| + t.Fatalf("the page varies with the request:\n%s\n---\n%s", rec.Body.String(), plain) |
| + } |
| +} |
| + |
| +func TestNoDisabledHookUnchanged(t *testing.T) { |
| + t.Parallel() |
| + s := newDisabledScene(t) |
| + rec := gateDo(s.e, http.MethodGet, disabledAlias, "/", "") |
| + if rec.Code != http.StatusOK || rec.Body.String() != "app" { |
| + t.Fatalf("no hook: %d %q", rec.Code, rec.Body.String()) |
| + } |
| + s.e.SetPlanDisabledFor(func(registry.Route, string) bool { return false }) |
| + if rec := gateDo(s.e, http.MethodGet, disabledHost, "/", ""); rec.Code != http.StatusOK { |
| + t.Fatalf("a hook answering false: %d", rec.Code) |
| + } |
| +} |
diff --git a/internal/host/agent.go b/internal/host/agent.go| index 4f78e06ed..916c032fa 100644 |
| --- a/internal/host/agent.go |
| +++ b/internal/host/agent.go |
| @@ -950,6 +950,9 @@ func runAgent(args []string, buildClaims ClaimFactory) { |
| // becomes awake-throttled. |
| e.SetPlanThrottleFor(planThrottleFor) |
| e.SetPlanGovernedFor(planGoverned) |
| + // What a lapse turned off (plans 4d), from the same snapshot: those |
| + // hosts get the disabled page. |
| + e.SetPlanDisabledFor(planDisabledFor) |
| closeUpgrades := func(sqid string) { |
| if n := e.CloseAccountUpgrades(sqid); n > 0 { |
| log.Printf("agent: plan throttle: closed %d open connection(s) of account %s", n, sqid) |
diff --git a/internal/host/plans_disabled_test.go b/internal/host/plans_disabled_test.go| new file mode 100644 |
| index 000000000..cdc3ebdec |
| --- /dev/null |
| +++ b/internal/host/plans_disabled_test.go |
| @@ -0,0 +1,101 @@ |
| +package host |
| + |
| +// Serial: these write the package-level plans cache. |
| + |
| +import ( |
| + "context" |
| + "testing" |
| + |
| + "github.com/carlosframework/platform/internal/plans" |
| + "github.com/carlosframework/platform/internal/registry" |
| +) |
| + |
| +// putDisabledLimits writes sqid's record listing turned-off hosts. |
| +func putDisabledLimits(t *testing.T, s *plansCountingStore, sqid string, hosts ...string) { |
| + t.Helper() |
| + ctx := context.Background() |
| + _, etag, err := plans.GetLimits(ctx, s, sqid) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + r := &plans.Record{Account: sqid, CatalogID: "v1@0123456789ab", Plan: "free", Revision: 1, Disabled: hosts} |
| + if _, err := plans.PutLimits(ctx, s, r, etag); err != nil { |
| + t.Fatal(err) |
| + } |
| +} |
| + |
| +func TestPlansPassKeepsDisabled(t *testing.T) { |
| + s, reg := plansWorld(t) |
| + putMarker(t, s, true) |
| + putDisabledLimits(t, s, "bab", "a.example") |
| + putDisabledLimits(t, s, "bdk") |
| + plansPass(context.Background(), s, reg, plansNoEnv) |
| + if !planDisabledFor(throttledRoute, "a.example") { |
| + t.Fatal("a listed route is not disabled") |
| + } |
| + if planDisabledFor(otherRoute, "b.example") { |
| + t.Fatal("another account's route is disabled") |
| + } |
| + // Another account cannot list a host it does not own. |
| + putDisabledLimits(t, s, "bdk", "a.example") |
| + putDisabledLimits(t, s, "bab") |
| + plansPass(context.Background(), s, reg, plansNoEnv) |
| + if planDisabledFor(throttledRoute, "a.example") { |
| + t.Fatal("another account's record disabled bab's route") |
| + } |
| +} |
| + |
| +func TestPlanDisabledForMatchesAliasAndCatchall(t *testing.T) { |
| + t.Cleanup(func() { planLimits.Store(nil) }) |
| + planLimits.Store(&planLimitsSnapshot{ |
| + byAccount: map[string]plans.Limits{"bab": {}}, |
| + throttle: map[string]plans.Throttle{}, |
| + disabled: map[string]map[string]bool{"bab": {"shop.example.com": true, "*.example.com": true}}, |
| + }) |
| + rt := registry.Route{Host: "primary.example", Kind: "instance", Account: "bab", App: "web"} |
| + for host, want := range map[string]bool{ |
| + "Shop.Example.com": true, |
| + "a.example.com": true, |
| + "b.a.example.com": false, |
| + "primary.example": false, |
| + "example.com": false, |
| + "other.example.org": false, |
| + } { |
| + if got := planDisabledFor(rt, host); got != want { |
| + t.Errorf("planDisabledFor(%q) = %v, want %v", host, got, want) |
| + } |
| + } |
| +} |
| + |
| +func TestDisabledKeptOnReadError(t *testing.T) { |
| + s, reg := plansWorld(t) |
| + putMarker(t, s, true) |
| + putDisabledLimits(t, s, "bab", "a.example") |
| + plansPass(context.Background(), s, reg, plansNoEnv) |
| + s.fail[plans.LimitsKey("bab")] = true |
| + plansPass(context.Background(), s, reg, plansNoEnv) |
| + if !planDisabledFor(throttledRoute, "a.example") { |
| + t.Fatal("a read error dropped the disabled list") |
| + } |
| +} |
| + |
| +func TestDisabledOffWithPlanLimitsOff(t *testing.T) { |
| + s, reg := plansWorld(t) |
| + putMarker(t, s, true) |
| + putDisabledLimits(t, s, "bab", "a.example") |
| + plansPass(context.Background(), s, reg, func(k string) string { |
| + if k == "CARLOS_PLAN_LIMITS" { |
| + return "off" |
| + } |
| + return "" |
| + }) |
| + if planDisabledFor(throttledRoute, "a.example") { |
| + t.Fatal("disabled with CARLOS_PLAN_LIMITS=off") |
| + } |
| + // No marker: nothing disabled either. |
| + putMarker(t, s, false) |
| + plansPass(context.Background(), s, reg, plansNoEnv) |
| + if planDisabledFor(throttledRoute, "a.example") { |
| + t.Fatal("disabled without the marker") |
| + } |
| +} |
diff --git a/internal/host/plans_pass.go b/internal/host/plans_pass.go| index efd496870..9c99c49ea 100644 |
| --- a/internal/host/plans_pass.go |
| +++ b/internal/host/plans_pass.go |
| @@ -31,6 +31,9 @@ type planLimitsSnapshot struct { |
| // throttle is each account's plan throttle as published (plans 4c); |
| // readers check Active, so an expired one slows nothing. |
| throttle map[string]plans.Throttle |
| + // disabled is each account's turned-off hosts as published (plans 4d), |
| + // lower case. |
| + disabled map[string]map[string]bool |
| } |
| var planLimits atomic.Pointer[planLimitsSnapshot] |
| @@ -90,7 +93,8 @@ func plansPass(ctx context.Context, s release.Store, reg *registry.Registry, get |
| return |
| } |
| prev := planLimits.Load() |
| - next := &planLimitsSnapshot{byAccount: map[string]plans.Limits{}, throttle: map[string]plans.Throttle{}} |
| + next := &planLimitsSnapshot{byAccount: map[string]plans.Limits{}, throttle: map[string]plans.Throttle{}, |
| + disabled: map[string]map[string]bool{}} |
| seen := map[string]bool{} |
| failed := false |
| for _, rt := range routes { |
| @@ -114,12 +118,22 @@ func plansPass(ctx context.Context, s release.Store, reg *registry.Registry, get |
| if th, ok := prev.throttle[sqid]; ok { |
| next.throttle[sqid] = th |
| } |
| + if d, ok := prev.disabled[sqid]; ok { |
| + next.disabled[sqid] = d |
| + } |
| } |
| case rec != nil: |
| next.byAccount[sqid] = rec.Limits |
| if rec.Throttle != nil { |
| next.throttle[sqid] = *rec.Throttle |
| } |
| + if len(rec.Disabled) > 0 { |
| + set := make(map[string]bool, len(rec.Disabled)) |
| + for _, h := range rec.Disabled { |
| + set[strings.ToLower(h)] = true |
| + } |
| + next.disabled[sqid] = set |
| + } |
| } |
| } |
| planLimits.Store(next) |
| @@ -222,3 +236,33 @@ func planMaxRunFor(rt registry.Route) time.Duration { |
| } |
| return 0 |
| } |
| + |
| +// planDisabledFor is whether a lapse turned off what a request reaches |
| +// (plans 4d): plans on, the route's account has a record, and the route's |
| +// own host or the request host is on its turned-off list, exactly or by |
| +// "*."+parent(host) for a catch-all. Only the route's own account's list |
| +// counts. Anything else is false: a box that cannot tell serves (spec §7). |
| +func planDisabledFor(rt registry.Route, host string) bool { |
| + snap := planLimits.Load() |
| + if snap == nil { |
| + return false |
| + } |
| + set := snap.disabled[release.RouteAccount(rt)] |
| + if len(set) == 0 { |
| + return false |
| + } |
| + if set[strings.ToLower(rt.Host)] { |
| + return true |
| + } |
| + host = strings.ToLower(strings.TrimSuffix(host, ".")) |
| + if host == "" { |
| + return false |
| + } |
| + if set[host] { |
| + return true |
| + } |
| + if _, parent, ok := strings.Cut(host, "."); ok && strings.Contains(parent, ".") { |
| + return set["*."+parent] |
| + } |
| + return false |
| +} |