carlos / platform Public

host: keep each account's plan throttle from its limits record (plans 4c)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 6215723e0ed6aa712148a65b3be0af449620bb44 parent d65f4f6
2 files changed, +133 −1
  • internal/host/plans_pass.go +41 −1
  • internal/host/plans_throttle_test.go +92 −0
diff --git a/internal/host/plans_pass.go b/internal/host/plans_pass.go
index 4130da030..c3f5986f3 100644
--- a/internal/host/plans_pass.go
+++ b/internal/host/plans_pass.go
@@ -28,6 +28,9 @@ import (
// are off on this box.
type planLimitsSnapshot struct {
byAccount map[string]plans.Limits
+ // 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
}
var planLimits atomic.Pointer[planLimitsSnapshot]
@@ -87,7 +90,7 @@ func plansPass(ctx context.Context, s release.Store, reg *registry.Registry, get
return
}
prev := planLimits.Load()
- next := &planLimitsSnapshot{byAccount: map[string]plans.Limits{}}
+ next := &planLimitsSnapshot{byAccount: map[string]plans.Limits{}, throttle: map[string]plans.Throttle{}}
seen := map[string]bool{}
failed := false
for _, rt := range routes {
@@ -108,9 +111,15 @@ func plansPass(ctx context.Context, s release.Store, reg *registry.Registry, get
if l, ok := prev.byAccount[sqid]; ok {
next.byAccount[sqid] = l
}
+ if th, ok := prev.throttle[sqid]; ok {
+ next.throttle[sqid] = th
+ }
}
case rec != nil:
next.byAccount[sqid] = rec.Limits
+ if rec.Throttle != nil {
+ next.throttle[sqid] = *rec.Throttle
+ }
}
}
planLimits.Store(next)
@@ -147,3 +156,34 @@ func planRateCeilingFor(rt registry.Route) ratelimit.Ceiling {
}
return l.RateCeiling()
}
+
+// planAwakeCeiling is the continuous-awake ceiling for an awake-throttled
+// account's instances (spec §5.2).
+const planAwakeCeiling = 15 * time.Minute
+
+// planThrottleFor is the route's account's plan throttle when one is in
+// force: an instance route, plans on, a record with a throttle, before its
+// Until. Anything else is (zero, false).
+func planThrottleFor(rt registry.Route) (plans.Throttle, bool) {
+ if rt.Kind != "instance" {
+ return plans.Throttle{}, false
+ }
+ snap := planLimits.Load()
+ if snap == nil {
+ return plans.Throttle{}, false
+ }
+ th, ok := snap.throttle[release.RouteAccount(rt)]
+ if !ok || !th.Active(time.Now()) {
+ return plans.Throttle{}, false
+ }
+ return th, true
+}
+
+// planMaxRunFor is the activator's continuous-awake hook: 15 minutes for an
+// instance of an awake-throttled account, else 0 (no ceiling).
+func planMaxRunFor(rt registry.Route) time.Duration {
+ if th, ok := planThrottleFor(rt); ok && th.Awake {
+ return planAwakeCeiling
+ }
+ return 0
+}
diff --git a/internal/host/plans_throttle_test.go b/internal/host/plans_throttle_test.go
new file mode 100644
index 000000000..2201dda2b
--- /dev/null
+++ b/internal/host/plans_throttle_test.go
@@ -0,0 +1,92 @@
+package host
+
+// Serial: these write the package-level plans cache.
+
+import (
+ "context"
+ "testing"
+ "time"
+
+ "github.com/carlosframework/platform/internal/plans"
+ "github.com/carlosframework/platform/internal/registry"
+)
+
+// putThrottledLimits writes sqid's record with a throttle.
+func putThrottledLimits(t *testing.T, s *plansCountingStore, sqid string, th *plans.Throttle) {
+ 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, Throttle: th}
+ if _, err := plans.PutLimits(ctx, s, r, etag); err != nil {
+ t.Fatal(err)
+ }
+}
+
+var (
+ throttledRoute = registry.Route{Host: "a.example", Kind: "instance", Account: "bab", App: "web"}
+ otherRoute = registry.Route{Host: "b.example", Kind: "instance", Account: "bdk", App: "shop"}
+)
+
+func TestPlansPassKeepsThrottle(t *testing.T) {
+ s, reg := plansWorld(t)
+ putMarker(t, s, true)
+ putThrottledLimits(t, s, "bab", &plans.Throttle{Bandwidth: true, Until: time.Now().Add(24 * time.Hour)})
+ putAccountLimits(t, s, "bdk", 90)
+ plansPass(context.Background(), s, reg, plansNoEnv)
+ if th, ok := planThrottleFor(throttledRoute); !ok || !th.Bandwidth {
+ t.Fatalf("bab throttle = %+v, %v", th, ok)
+ }
+ if _, ok := planThrottleFor(otherRoute); ok {
+ t.Fatal("an unthrottled account is throttled")
+ }
+}
+
+func TestExpiredThrottleIsIgnored(t *testing.T) {
+ s, reg := plansWorld(t)
+ putMarker(t, s, true)
+ putThrottledLimits(t, s, "bab", &plans.Throttle{Bandwidth: true, Awake: true, Until: time.Now().Add(-time.Minute)})
+ plansPass(context.Background(), s, reg, plansNoEnv)
+ if _, ok := planThrottleFor(throttledRoute); ok {
+ t.Fatal("a throttle past Until still applies")
+ }
+ if d := planMaxRunFor(throttledRoute); d != 0 {
+ t.Fatalf("an expired awake throttle gives a ceiling of %v", d)
+ }
+}
+
+func TestPlanMaxRunFor(t *testing.T) {
+ t.Cleanup(func() { planLimits.Store(nil) })
+ until := time.Now().Add(time.Hour)
+ planLimits.Store(&planLimitsSnapshot{byAccount: map[string]plans.Limits{}, throttle: map[string]plans.Throttle{
+ "bab": {Awake: true, Until: until}, "bdk": {Bandwidth: true, Until: until},
+ }})
+ if d := planMaxRunFor(throttledRoute); d != 15*time.Minute {
+ t.Fatalf("awake-throttled instance: %v, want 15m", d)
+ }
+ if d := planMaxRunFor(otherRoute); d != 0 {
+ t.Fatalf("bandwidth-only throttle: %v, want 0", d)
+ }
+ worker := throttledRoute
+ worker.Kind = "worker"
+ if d := planMaxRunFor(worker); d != 0 {
+ t.Fatalf("worker route: %v, want 0", d)
+ }
+}
+
+func TestThrottleOffWithPlanLimitsOff(t *testing.T) {
+ s, reg := plansWorld(t)
+ putMarker(t, s, true)
+ putThrottledLimits(t, s, "bab", &plans.Throttle{Bandwidth: true, Until: time.Now().Add(time.Hour)})
+ plansPass(context.Background(), s, reg, func(k string) string {
+ if k == "CARLOS_PLAN_LIMITS" {
+ return "off"
+ }
+ return ""
+ })
+ if _, ok := planThrottleFor(throttledRoute); ok {
+ t.Fatal("throttled with CARLOS_PLAN_LIMITS=off")
+ }
+}