plans, console: per-account throttle on the plan row and the limits record (plans 4c)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
7 files changed,
+160
−3
internal/console/accountplans.go+35 −3internal/console/planlimits.go+1 −0internal/console/planlimits_test.go+45 −0internal/console/store.go+2 −0internal/plans/record.go+3 −0internal/plans/throttle.go+28 −0internal/plans/throttle_test.go+46 −0
diff --git a/internal/console/accountplans.go b/internal/console/accountplans.go| index 4917eb1b7..09abdd9fc 100644 |
| --- a/internal/console/accountplans.go |
| +++ b/internal/console/accountplans.go |
| @@ -39,19 +39,27 @@ type AccountPlanRow struct { |
| PublishedCatalog string |
| UpdatedBy string |
| UpdatedAt time.Time |
| + // Throttle is the account's plan throttle (plans 4c); nil for none. |
| + Throttle *plans.Throttle |
| } |
| const accountPlanColumns = `p.account_id, a.sqid, p.plan, p.packs, p.source, p.sponsored, p.reason, |
| - p.revision, p.published_revision, p.published_catalog, p.updated_by, p.updated_at` |
| + p.revision, p.published_revision, p.published_catalog, p.updated_by, p.updated_at, p.throttle` |
| func scanAccountPlan(sc interface{ Scan(...any) error }) (*AccountPlanRow, error) { |
| var r AccountPlanRow |
| - var packs, sponsored string |
| + var packs, sponsored, throttle string |
| var updated int64 |
| if err := sc.Scan(&r.AccountID, &r.Sqid, &r.Plan, &packs, &r.Source, &sponsored, &r.Reason, |
| - &r.Revision, &r.PublishedRevision, &r.PublishedCatalog, &r.UpdatedBy, &updated); err != nil { |
| + &r.Revision, &r.PublishedRevision, &r.PublishedCatalog, &r.UpdatedBy, &updated, &throttle); err != nil { |
| return nil, err |
| } |
| + if throttle != "" { |
| + r.Throttle = &plans.Throttle{} |
| + if err := json.Unmarshal([]byte(throttle), r.Throttle); err != nil { |
| + return nil, err |
| + } |
| + } |
| if err := json.Unmarshal([]byte(packs), &r.Packs); err != nil { |
| return nil, err |
| } |
| @@ -182,3 +190,27 @@ func (s *Store) RestoringAppsIn(accountID int64) ([]string, error) { |
| } |
| return out, rows.Err() |
| } |
| + |
| +// SetAccountThrottle stores the account's throttle (nil clears it) and |
| +// bumps its revision so the publish pass carries it to the boxes. An |
| +// unchanged throttle writes nothing. An account with no plan row yet is |
| +// (false, nil): the default-plan pass gives it one, and the next grace |
| +// pass sets the throttle then. |
| +func (s *Store) SetAccountThrottle(accountID int64, t *plans.Throttle, now time.Time) (bool, error) { |
| + val := "" |
| + if t != nil { |
| + t.Until = t.Until.UTC() |
| + b, err := json.Marshal(t) |
| + if err != nil { |
| + return false, err |
| + } |
| + val = string(b) |
| + } |
| + res, err := s.db.Exec(`UPDATE account_plans SET throttle = ?, revision = revision + 1, updated_at = ? |
| + WHERE account_id = ? AND throttle <> ?`, val, now.Unix(), accountID, val) |
| + if err != nil { |
| + return false, err |
| + } |
| + n, err := res.RowsAffected() |
| + return n > 0, err |
| +} |
diff --git a/internal/console/planlimits.go b/internal/console/planlimits.go| index 813f0020c..dd9456831 100644 |
| --- a/internal/console/planlimits.go |
| +++ b/internal/console/planlimits.go |
| @@ -92,6 +92,7 @@ func (s *Server) publishAccountLimits(ctx context.Context, row AccountPlanRow, n |
| Revision: row.Revision, |
| Limits: limits, |
| IssuedAt: now.UTC().Truncate(time.Second), |
| + Throttle: row.Throttle, |
| } |
| for attempt := 0; attempt < 2; attempt++ { |
| cur, etag, err := plans.GetLimits(ctx, s.cfg.Releases, row.Sqid) |
diff --git a/internal/console/planlimits_test.go b/internal/console/planlimits_test.go| index fb81019c2..5aa66783f 100644 |
| --- a/internal/console/planlimits_test.go |
| +++ b/internal/console/planlimits_test.go |
| @@ -238,3 +238,48 @@ func TestPlanLimitsPassOrphansDoNotStarveLaterAccounts(t *testing.T) { |
| t.Fatal("orphaned accounts starved a later valid account") |
| } |
| } |
| + |
| +func TestSetAccountThrottleBumpsRevisionOnChange(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + acct := planAccount(t, fx, 501) |
| + if _, err := fx.st.SetAccountPlan(acct.ID, "free", nil, PlanSourceOperator, nil, "r", "ops@example.com", planNow); err != nil { |
| + t.Fatal(err) |
| + } |
| + before, _ := fx.st.AccountPlan(acct.ID) |
| + th := &plans.Throttle{Bandwidth: true, Until: time.Date(2027, 2, 1, 0, 0, 0, 0, time.UTC)} |
| + if changed, err := fx.st.SetAccountThrottle(acct.ID, th, planNow); err != nil || !changed { |
| + t.Fatalf("set = %v, %v", changed, err) |
| + } |
| + row, _ := fx.st.AccountPlan(acct.ID) |
| + if row.Revision != before.Revision+1 || row.Throttle == nil || !row.Throttle.Bandwidth { |
| + t.Fatalf("row after set = %+v", row) |
| + } |
| + if changed, err := fx.st.SetAccountThrottle(acct.ID, th, planNow); err != nil || changed { |
| + t.Fatalf("same throttle again = %v, %v, want unchanged", changed, err) |
| + } |
| + if again, _ := fx.st.AccountPlan(acct.ID); again.Revision != row.Revision { |
| + t.Fatalf("revision moved on an unchanged throttle: %d -> %d", row.Revision, again.Revision) |
| + } |
| + if changed, err := fx.st.SetAccountThrottle(acct.ID, nil, planNow); err != nil || !changed { |
| + t.Fatalf("clear = %v, %v", changed, err) |
| + } |
| + if cleared, _ := fx.st.AccountPlan(acct.ID); cleared.Throttle != nil { |
| + t.Fatalf("cleared row still throttled: %+v", cleared.Throttle) |
| + } |
| +} |
| + |
| +func TestPublishCarriesThrottle(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + fx.srv.cfg.PlanCatalog = examplePlanCatalog(t) |
| + acct := planAccount(t, fx, 501) |
| + fx.srv.planLimitsPass(context.Background(), planNow) |
| + th := &plans.Throttle{Awake: true, Until: time.Date(2027, 2, 1, 0, 0, 0, 0, time.UTC)} |
| + if _, err := fx.st.SetAccountThrottle(acct.ID, th, planNow); err != nil { |
| + t.Fatal(err) |
| + } |
| + fx.srv.planLimitsPass(context.Background(), planNow.Add(time.Minute)) |
| + r, _ := readLimits(t, fx, acct.Sqid) |
| + if r == nil || r.Throttle == nil || !r.Throttle.Awake || !r.Throttle.Until.Equal(th.Until) { |
| + t.Fatalf("published record = %+v", r) |
| + } |
| +} |
diff --git a/internal/console/store.go b/internal/console/store.go| index 17a8619b5..5049a2b4f 100644 |
| --- a/internal/console/store.go |
| +++ b/internal/console/store.go |
| @@ -422,6 +422,8 @@ func (s *Store) migrate() error { |
| // re-running is always safe. |
| for _, stmt := range []string{ |
| `ALTER TABLE users ADD COLUMN operator INTEGER NOT NULL DEFAULT 0;`, |
| + // Plans 4c: the account's throttle, JSON, "" for none. |
| + `ALTER TABLE account_plans ADD COLUMN throttle TEXT NOT NULL DEFAULT '';`, |
| // deliveries (tenant-email, ruling 2a): SES already publishes |
| // Delivery to the event destination and the ingest already |
| // verifies and parses it — this stops throwing the answer away. |
diff --git a/internal/plans/record.go b/internal/plans/record.go| index fd8314757..e5077ba7e 100644 |
| --- a/internal/plans/record.go |
| +++ b/internal/plans/record.go |
| @@ -29,6 +29,9 @@ type Record struct { |
| Revision int64 `json:"revision"` |
| Limits Limits `json:"limits"` |
| IssuedAt time.Time `json:"issued_at"` |
| + // Throttle, when set, slows the account for the rest of the month |
| + // (plans 4c). |
| + Throttle *Throttle `json:"throttle,omitempty"` |
| } |
| // LimitsKey is where an account's limits record lives. |
diff --git a/internal/plans/throttle.go b/internal/plans/throttle.go| new file mode 100644 |
| index 000000000..6c4d84f4b |
| --- /dev/null |
| +++ b/internal/plans/throttle.go |
| @@ -0,0 +1,28 @@ |
| +package plans |
| + |
| +import ( |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/ratelimit" |
| +) |
| + |
| +// Throttle is an account slowed down for the rest of the month (plans 4c, |
| +// spec §5.2 and §5.7): past its monthly bandwidth (Bandwidth) or awake |
| +// hours (Awake). The console sets it; boxes read it from the limits record |
| +// and drop it after Until, the first instant of the next UTC month, even |
| +// if the console has stopped writing. |
| +type Throttle struct { |
| + Bandwidth bool `json:"bandwidth,omitempty"` |
| + Awake bool `json:"awake,omitempty"` |
| + Until time.Time `json:"until"` |
| +} |
| + |
| +// Active is whether t slows anything at now. |
| +func (t *Throttle) Active(now time.Time) bool { |
| + return t != nil && (t.Bandwidth || t.Awake) && now.Before(t.Until) |
| +} |
| + |
| +// ThrottleRule is a throttled account's one shared request bucket across |
| +// all its instances: enough to load a page and read why, too little to run |
| +// a service (Paul, 2026-10-25). |
| +var ThrottleRule = ratelimit.Rule{Rate: 5, Burst: 30} |
diff --git a/internal/plans/throttle_test.go b/internal/plans/throttle_test.go| new file mode 100644 |
| index 000000000..66b0e917f |
| --- /dev/null |
| +++ b/internal/plans/throttle_test.go |
| @@ -0,0 +1,46 @@ |
| +package plans |
| + |
| +import ( |
| + "context" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/release" |
| +) |
| + |
| +func TestThrottleActive(t *testing.T) { |
| + now := time.Date(2026, 10, 25, 12, 0, 0, 0, time.UTC) |
| + until := time.Date(2026, 11, 1, 0, 0, 0, 0, time.UTC) |
| + var none *Throttle |
| + if none.Active(now) { |
| + t.Error("nil throttle active") |
| + } |
| + if !(&Throttle{Bandwidth: true, Until: until}).Active(now) { |
| + t.Error("bandwidth throttle before Until not active") |
| + } |
| + if (&Throttle{Awake: true, Until: until}).Active(until) { |
| + t.Error("throttle active at Until") |
| + } |
| + if (&Throttle{Until: until}).Active(now) { |
| + t.Error("throttle with neither flag active") |
| + } |
| +} |
| + |
| +func TestRecordRoundTripsThrottle(t *testing.T) { |
| + ctx := context.Background() |
| + s := release.NewDirStore(t.TempDir()) |
| + until := time.Date(2026, 11, 1, 0, 0, 0, 0, time.UTC) |
| + rec := &Record{Account: "abc", CatalogID: "c", Plan: "free", Source: "default", Revision: 1, |
| + Throttle: &Throttle{Awake: true, Until: until}} |
| + if _, err := PutLimits(ctx, s, rec, ""); err != nil { |
| + t.Fatal(err) |
| + } |
| + got, _, err := GetLimits(ctx, s, "abc") |
| + if err != nil || got.Throttle == nil || !got.Throttle.Awake || got.Throttle.Bandwidth || !got.Throttle.Until.Equal(until) { |
| + t.Fatalf("round trip = %+v, %v", got, err) |
| + } |
| + plain, err := ParseLimits([]byte(`{"schema":1,"account":"abc","catalog_id":"c","plan":"free","source":"default","revision":1,"limits":{}}`), "abc") |
| + if err != nil || plain.Throttle != nil { |
| + t.Fatalf("record without throttle = %+v, %v", plain, err) |
| + } |
| +} |