carlos / platform Public

fleet-routes-followups: edge deferred minors (#457 1a, Task 5)

- TestForwardUpgradeCloseCancelsTheRequest: CloseAccountUpgrades cancels
  the tracked forward-row request's context, not only counts it (the old
  test passed with a CloseAccountUpgrades that never cancelled; the new
  one catches that mutation).
- TestForwardPlanThrottlePlainHTTPAtTheHub: the plain HTTP plan-throttle
  path for a forward row, through the hub and tunnel: the rule's burst
  reaches the app, the next is the hub's 429. Catches a mutation that
  skips planThrottleAdmit for forward rows on the HTTP path only.
- TestForwardPlanThrottleExemptsTheTunnelAtTheBox: the fleet box's
  planThrottleOf fromTunnel exemption, mutation-tested (dropping it fails).
  The upgrade side of the box exemption waits on #471.
- policySceneRates: the policy scene with the rows' rate limits given and
  both plan limiters on the frozen clock.
- rateAdmit doc comment rewrapped to the file's width.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev ad3f05876c6afd1df3c89305aa1edce97d252b54 parent e25a0b3
2 files changed, +121 −15
  • internal/edge/fleetforward_policy_test.go +115 −9
  • internal/edge/ratelimit.go +6 −6
diff --git a/internal/edge/fleetforward_policy_test.go b/internal/edge/fleetforward_policy_test.go
index 6ac865621..72bc8e0ea 100644
--- a/internal/edge/fleetforward_policy_test.go
+++ b/internal/edge/fleetforward_policy_test.go
@@ -13,6 +13,8 @@ package edge
import (
"bufio"
+ "context"
+ "errors"
"fmt"
"io"
"net"
@@ -84,29 +86,46 @@ func (b *polBackend) counts() (gets, upgrades int) {
// polHost carries polHubRate, the box's own row polBoxRate, and both edges'
// limiters are on a frozen clock.
func policyScene(t *testing.T) (hubURL string, hub, box *Edge, app *polBackend) {
+ t.Helper()
+ return policySceneRates(t, polHubRate, polBoxRate)
+}
+
+// policySceneRates is policyScene with the two rows' rate limits given, so a
+// plan-throttle test can keep the per-instance limit out of its way. Both
+// edges' plan limiters are on the same frozen clock as their rate limiters.
+func policySceneRates(t *testing.T, hubRate, boxRate string) (hubURL string, hub, box *Edge, app *polBackend) {
t.Helper()
t.Setenv("CARLOS_RATE_LIMIT", "enforce")
app = &polBackend{closed: make(chan struct{}, 8)}
appAddr := startPolBackend(t, app)
fwd := forwardRow(polHost, "home", fwdOwner)
- fwd.RateLimit = polHubRate
+ fwd.RateLimit = hubRate
hubURL, hub, box = forwardHubAndBox(t, []registry.Route{fwd},
- []registry.Route{{Host: polHost, Kind: "instance", Status: "ready", Addr: appAddr, Account: fwdOwner, App: "shop", RateLimit: polBoxRate}},
+ []registry.Route{{Host: polHost, Kind: "instance", Status: "ready", Addr: appAddr, Account: fwdOwner, App: "shop", RateLimit: boxRate}},
nil)
for name, e := range map[string]*Edge{"hub": hub, "box": box} {
if e.rateMode != RateEnforce {
t.Fatalf("the %s's rate mode is %v, want enforce", name, e.rateMode)
}
}
- if got := hub.current()[polHost].rate; got.Burst != 2 {
- t.Fatalf("the hub's forward row parsed to %+v, want burst 2 (the row's RateLimit)", got)
- }
- if got := box.current()[polHost].rate; got.Burst != 1 {
- t.Fatalf("the box's row parsed to %+v, want burst 1", got)
+ for _, c := range []struct {
+ name string
+ e *Edge
+ col string
+ }{{"hub's forward row", hub, hubRate}, {"box's row", box, boxRate}} {
+ want, err := ratelimit.Parse(c.col)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if got := c.e.current()[polHost].rate; got.Burst != want.Burst {
+ t.Fatalf("the %s parsed to %+v, want burst %d (the row's RateLimit)", c.name, got, want.Burst)
+ }
}
frozen := time.Date(2026, 10, 30, 12, 0, 0, 0, time.UTC)
- hub.rateLimiter = ratelimit.NewLimiter(func() time.Time { return frozen }, 1000)
- box.rateLimiter = ratelimit.NewLimiter(func() time.Time { return frozen }, 1000)
+ for _, e := range []*Edge{hub, box} {
+ e.rateLimiter = ratelimit.NewLimiter(func() time.Time { return frozen }, 1000)
+ e.planLimiter = ratelimit.NewLimiter(func() time.Time { return frozen }, 1000)
+ }
return hubURL, hub, box, app
}
@@ -257,3 +276,90 @@ func TestForwardPlanThrottleAndUpgradeClose(t *testing.T) {
t.Fatalf("CloseAccountUpgrades after the handshake ended closed %d, want 0", n)
}
}
+
+// polWideRate keeps the per-instance limit out of a plan-throttle test's
+// way: a burst far above plans.ThrottleRule's, on both rows.
+const polWideRate = `{"rate":1,"burst":500}`
+
+// TestForwardPlanThrottlePlainHTTPAtTheHub (deferred minor, Task 5): a plain
+// request to a forward row spends the account's plan-throttle bucket at the
+// hub. The rule's burst is admitted and reaches the app; the next request is
+// the hub's 429 and never crosses the tunnel.
+func TestForwardPlanThrottlePlainHTTPAtTheHub(t *testing.T) {
+ hubURL, hub, _, app := policySceneRates(t, polWideRate, polWideRate)
+ hub.SetPlanThrottleFor(func(rt registry.Route) (plans.Throttle, bool) {
+ if rt.Account != fwdOwner {
+ return plans.Throttle{}, false
+ }
+ return plans.Throttle{Bandwidth: true, Until: time.Now().Add(time.Hour)}, true
+ })
+ for i := 1; i <= plans.ThrottleRule.Burst; i++ {
+ resp, body := fwdDo(t, hubURL, "GET", polHost, "/", "", nil)
+ if resp.StatusCode != http.StatusOK || string(body) != "ok" {
+ t.Fatalf("GET %d: %d %q, want the app's 200 within the plan throttle's burst of %d", i, resp.StatusCode, body, plans.ThrottleRule.Burst)
+ }
+ }
+ resp, body := fwdDo(t, hubURL, "GET", polHost, "/", "", nil)
+ wantHubRefusal(t, resp, string(body), "a GET past the plan throttle's burst")
+ if gets, _ := app.counts(); gets != plans.ThrottleRule.Burst {
+ t.Fatalf("the app saw %d requests, want %d: the refused one never crosses the tunnel", gets, plans.ThrottleRule.Burst)
+ }
+}
+
+// TestForwardPlanThrottleExemptsTheTunnelAtTheBox (deferred minor, Task 5):
+// the fleet box keeps exempting what arrives over the tunnel from the plan
+// throttle (planThrottleOf's fromTunnel), so a throttle the box holds for
+// the account does not judge the hub's forwarded requests a second time. The
+// hub holds none here: every request past the rule's burst still reaches the
+// app.
+func TestForwardPlanThrottleExemptsTheTunnelAtTheBox(t *testing.T) {
+ hubURL, _, box, app := policySceneRates(t, polWideRate, polWideRate)
+ box.SetPlanThrottleFor(func(rt registry.Route) (plans.Throttle, bool) {
+ if rt.Account != fwdOwner {
+ return plans.Throttle{}, false
+ }
+ return plans.Throttle{Bandwidth: true, Until: time.Now().Add(time.Hour)}, true
+ })
+ n := plans.ThrottleRule.Burst + 5
+ for i := 1; i <= n; i++ {
+ resp, body := fwdDo(t, hubURL, "GET", polHost, "/", "", nil)
+ if resp.StatusCode != http.StatusOK || string(body) != "ok" {
+ t.Fatalf("GET %d: %d %q, want the app's 200: the box must not plan-throttle a tunneled request", i, resp.StatusCode, body)
+ }
+ }
+ if gets, _ := app.counts(); gets != n {
+ t.Fatalf("the app saw %d requests, want %d", gets, n)
+ }
+}
+
+// TestForwardUpgradeCloseCancelsTheRequest (deferred minor, Task 5):
+// CloseAccountUpgrades does not only count a forward row's tracked upgrade,
+// it cancels the context of the request it tracked, which is what ends the
+// proxied connection. Driven directly, since no upgrade stays open across the
+// tunnel (#471).
+func TestForwardUpgradeCloseCancelsTheRequest(t *testing.T) {
+ _, hub, _, _ := policyScene(t)
+ hub.SetPlanGovernedFor(func(rt registry.Route) bool { return rt.Account == fwdOwner })
+ up := hub.current()[polHost]
+ if !up.route.IsForward() {
+ t.Fatalf("the hub's row for %s is %+v, want its forward row", polHost, up.route)
+ }
+ req, err := http.NewRequest("GET", "http://"+polHost+"/socket", nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+ tracked, done := hub.trackUpgrade(req, up)
+ defer done()
+ if tracked.Context().Err() != nil {
+ t.Fatalf("the tracked request is done before any close: %v", tracked.Context().Err())
+ }
+ if n := hub.CloseAccountUpgrades(fwdOwner); n != 1 {
+ t.Fatalf("CloseAccountUpgrades(%q) = %d, want 1", fwdOwner, n)
+ }
+ if err := tracked.Context().Err(); !errors.Is(err, context.Canceled) {
+ t.Fatalf("after CloseAccountUpgrades the tracked request's context is %v, want canceled", err)
+ }
+ if req.Context().Err() != nil {
+ t.Fatal("CloseAccountUpgrades cancelled the caller's own context, not the tracked one")
+ }
+}
diff --git a/internal/edge/ratelimit.go b/internal/edge/ratelimit.go
index 10a08a268..a111d3601 100644
--- a/internal/edge/ratelimit.go
+++ b/internal/edge/ratelimit.go
@@ -117,12 +117,12 @@ func (e *Edge) parseRateSettings(rt registry.Route) ratelimit.Settings {
//
// Not judged: anything but an instance or a forward row (fleet public
// routes §4.8: the hub judges a forward row once, with the app's own
-// settings from the row's rate_limit column), a door caller (the box itself, on
-// this box or on the box that backhauled it, hopDoorFrom), and a request
-// that arrived over a fleet tunnel. On the fleet side of a tunnel every
-// request comes from the hub, so keying on it would put every client in one
-// bucket; the hub has the route row and the real client, and limits there
-// (its route is viaTunnel).
+// settings from the row's rate_limit column), a door caller (the box
+// itself, on this box or on the box that backhauled it, hopDoorFrom), and a
+// request that arrived over a fleet tunnel. On the fleet side of a tunnel
+// every request comes from the hub, so keying on it would put every client
+// in one bucket; the hub has the route row and the real client, and limits
+// there (its route is viaTunnel).
func (e *Edge) rateAdmit(w http.ResponseWriter, r *http.Request, up upstream) (release func(), ok bool) {
if e.rateMode == RateOff || (up.route.Kind != "instance" && !up.route.IsForward()) {
return nil, true