console: throttle an account past its monthly bandwidth or awake hours (plans 4c)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 files changed,
+222
−0
internal/console/plancaps_copy.go+5 −0internal/console/plangrace_view_test.go+15 −0internal/console/plangracepass.go+16 −0internal/console/planthrottle.go+68 −0internal/console/planthrottle_test.go+118 −0
diff --git a/internal/console/plancaps_copy.go b/internal/console/plancaps_copy.go| index 0552ab413..c31376390 100644 |
| --- a/internal/console/plancaps_copy.go |
| +++ b/internal/console/plancaps_copy.go |
| @@ -49,4 +49,9 @@ var planNoticeCopy = map[string]string{ |
| "cli.email_paused": "email is paused until midnight utc: this account sent %s", |
| "banner.storage_denied": "New uploads to this account's storage are refused: %s. Downloads and deletes still work.", |
| "cli.storage_denied": "new uploads to this account's storage are refused: %s. downloads and deletes still work", |
| + // Plans 4c: the monthly throttle. Luna pass pending. |
| + "banner.bandwidth_throttled": "This account is slowed down until the month ends: it has used %s.", |
| + "cli.bandwidth_throttled": "this account is slowed down until the month ends: it has used %s", |
| + "banner.awake_throttled": "This account's apps are slowed down and put to sleep after 15 minutes awake until the month ends: %s.", |
| + "cli.awake_throttled": "this account's apps are slowed down and put to sleep after 15 minutes awake until the month ends: %s", |
| } |
diff --git a/internal/console/plangrace_view_test.go b/internal/console/plangrace_view_test.go| index be2fbb535..a1ca6d53f 100644 |
| --- a/internal/console/plangrace_view_test.go |
| +++ b/internal/console/plangrace_view_test.go |
| @@ -119,3 +119,18 @@ func TestBannerSaysStorageWritesDenied(t *testing.T) { |
| t.Fatalf("no storage-denied banner:\n%s", body) |
| } |
| } |
| + |
| +func TestBannerSaysBandwidthThrottled(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + fx.srv.cfg.PlanCatalog = examplePlanCatalog(t) |
| + acct, _ := fx.st.AccountBySqid(fx.sqid) |
| + st := evaluateCap(nil, capBandwidth, 25_000_000_000, 10_737_418_240, time.Now()) // free: 10 GiB a month, 2x and over |
| + st.AccountID = acct.ID |
| + if err := fx.st.PutCapState(st); err != nil { |
| + t.Fatal(err) |
| + } |
| + c := signInPaul(t, fx) |
| + if _, _, body := get(t, c, fx.u("/")); !strings.Contains(body, "This account is slowed down until the month ends") { |
| + t.Fatalf("no bandwidth-throttled banner:\n%s", body) |
| + } |
| +} |
diff --git a/internal/console/plangracepass.go b/internal/console/plangracepass.go| index 3fc5ebd19..feb8d32e6 100644 |
| --- a/internal/console/plangracepass.go |
| +++ b/internal/console/plangracepass.go |
| @@ -99,6 +99,18 @@ func (s *Server) planGraceAccount(ctx context.Context, acct *Account, now time.T |
| } |
| } |
| } |
| + byState := map[capID]capState{} |
| + measuredCaps, offeredCaps := map[capID]bool{}, map[capID]bool{} |
| + for _, st := range states { |
| + byState[st.Cap] = st |
| + } |
| + for _, c := range capOrder { |
| + _, m := measures[c] |
| + measuredCaps[c], offeredCaps[c] = m, m || errs[c] != nil |
| + } |
| + if err := s.convergeThrottle(ctx, acct, byState, measuredCaps, offeredCaps, now); err != nil { |
| + log.Printf("console: plan grace %s: throttle: %v", acct.Sqid, err) |
| + } |
| if _, measured := measures[capStorage]; !measured && errs[capStorage] == nil { |
| // The plan sets no storage limit: nothing can be over it. |
| if err := s.clearStorageDeny(ctx, acct, now); err != nil { |
| @@ -352,6 +364,10 @@ func (s *Server) planStage(acct *Account, now time.Time) func(capState) string { |
| return "email_paused" |
| case st.Cap == capStorage && storageDenyDue(st, now): |
| return "storage_denied" |
| + case st.Cap == capBandwidth && bandwidthThrottleDue(st, now): |
| + return "bandwidth_throttled" |
| + case st.Cap == capAwakeHours && awakeThrottleDue(st, now): |
| + return "awake_throttled" |
| } |
| return "" |
| } |
diff --git a/internal/console/planthrottle.go b/internal/console/planthrottle.go| new file mode 100644 |
| index 000000000..284cf3f1a |
| --- /dev/null |
| +++ b/internal/console/planthrottle.go |
| @@ -0,0 +1,68 @@ |
| +package console |
| + |
| +import ( |
| + "context" |
| + "fmt" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/plans" |
| +) |
| + |
| +// bandwidthThrottleDue: the month's bandwidth has reached twice the limit, |
| +// table 4.2's hard ceiling, "then throttled", which does not wait for |
| +// notice (Paul, 2026-10-25). |
| +func bandwidthThrottleDue(st capState, now time.Time) bool { |
| + return st.Period == capPeriod(capBandwidth, now) && st.Level == levelCeiling |
| +} |
| + |
| +// awakeThrottleDue: the month's awake hours have reached twice the limit |
| +// and an owner has had notice for 72 hours. Awake hours have no hard |
| +// ceiling, so the notice rule holds (Paul, 2026-10-25). |
| +func awakeThrottleDue(st capState, now time.Time) bool { |
| + return st.Period == capPeriod(capAwakeHours, now) && st.Level == levelCeiling && |
| + !st.NoticeAt.IsZero() && !st.NoticeAt.After(now.Add(-72*time.Hour)) |
| +} |
| + |
| +// convergeThrottle sets the account's throttle from this pass's rows |
| +// (plans 4c). Per cap: measured decides from the predicate; not offered by |
| +// the plan means no throttle; offered but unmeasured keeps what is stored |
| +// (unknown is not over), unless the stored throttle has already expired. |
| +func (s *Server) convergeThrottle(ctx context.Context, acct *Account, states map[capID]capState, measured, offered map[capID]bool, now time.Time) error { |
| + if s.cfg.PlanCatalog == nil { |
| + return nil |
| + } |
| + row, err := s.store.AccountPlan(acct.ID) |
| + if err != nil || row == nil { |
| + return err |
| + } |
| + var bw, aw bool |
| + if row.Throttle.Active(now) { |
| + bw, aw = row.Throttle.Bandwidth, row.Throttle.Awake |
| + } |
| + decide := func(c capID, cur bool, due func(capState, time.Time) bool) bool { |
| + switch { |
| + case measured[c]: |
| + return due(states[c], now) |
| + case !offered[c]: |
| + return false |
| + } |
| + return cur |
| + } |
| + bw = decide(capBandwidth, bw, bandwidthThrottleDue) |
| + aw = decide(capAwakeHours, aw, awakeThrottleDue) |
| + var next *plans.Throttle |
| + if bw || aw { |
| + next = &plans.Throttle{Bandwidth: bw, Awake: aw, Until: graceEnd(capBandwidth, now)} |
| + } |
| + changed, err := s.store.SetAccountThrottle(acct.ID, next, now) |
| + if err != nil || !changed { |
| + return err |
| + } |
| + if next == nil { |
| + s.audit(acct, "", "plan", "plan.unthrottled", "plan: the account is no longer slowed down") |
| + return nil |
| + } |
| + s.audit(acct, "", "plan", "plan.throttled", fmt.Sprintf("plan: slowed down until %s (bandwidth %v, awake hours %v)", |
| + next.Until.Format("2 January 2006"), bw, aw)) |
| + return nil |
| +} |
diff --git a/internal/console/planthrottle_test.go b/internal/console/planthrottle_test.go| new file mode 100644 |
| index 000000000..bac62f425 |
| --- /dev/null |
| +++ b/internal/console/planthrottle_test.go |
| @@ -0,0 +1,118 @@ |
| +package console |
| + |
| +import ( |
| + "context" |
| + "strings" |
| + "testing" |
| + "time" |
| +) |
| + |
| +func monthRow(c capID, used, limit int64, at time.Time) capState { |
| + return evaluateCap(nil, c, used, limit, at) |
| +} |
| + |
| +func TestBandwidthThrottleDue(t *testing.T) { |
| + now := planNow |
| + if bandwidthThrottleDue(monthRow(capBandwidth, 150, 100, now), now) { |
| + t.Error("due when over but under 2x") |
| + } |
| + if !bandwidthThrottleDue(monthRow(capBandwidth, 200, 100, now), now) { |
| + t.Error("not due at 2x without notice") |
| + } |
| + last := monthRow(capBandwidth, 300, 100, now.AddDate(0, -1, 0)) |
| + if bandwidthThrottleDue(last, now) { |
| + t.Error("last month's ceiling row due this month") |
| + } |
| +} |
| + |
| +func TestAwakeThrottleDue(t *testing.T) { |
| + now := planNow |
| + st := monthRow(capAwakeHours, 200, 100, now.Add(-100*time.Hour)) |
| + if awakeThrottleDue(st, now) { |
| + t.Error("due at 2x without notice") |
| + } |
| + st.NoticeAt = now.Add(-71 * time.Hour) |
| + if awakeThrottleDue(st, now) { |
| + t.Error("due with notice 71h old") |
| + } |
| + st.NoticeAt = now.Add(-72 * time.Hour) |
| + if !awakeThrottleDue(st, now) { |
| + t.Error("not due at 2x with notice 72h old") |
| + } |
| +} |
| + |
| +func throttleFixture(t *testing.T) (*consoleFixture, *Account) { |
| + t.Helper() |
| + fx := newConsoleFixture(t) |
| + fx.srv.cfg.PlanCatalog = examplePlanCatalog(t) |
| + acct, _ := fx.st.AccountBySqid(fx.sqid) |
| + if _, err := fx.st.SetAccountPlan(acct.ID, "free", nil, PlanSourceOperator, nil, "r", "ops@example.com", planNow); err != nil { |
| + t.Fatal(err) |
| + } |
| + return fx, acct |
| +} |
| + |
| +func both(v bool) map[capID]bool { return map[capID]bool{capBandwidth: v, capAwakeHours: v} } |
| + |
| +func TestThrottleSetAndCleared(t *testing.T) { |
| + fx, acct := throttleFixture(t) |
| + ctx := context.Background() |
| + states := map[capID]capState{capBandwidth: monthRow(capBandwidth, 200, 100, planNow)} |
| + if err := fx.srv.convergeThrottle(ctx, acct, states, map[capID]bool{capBandwidth: true}, both(true), planNow); err != nil { |
| + t.Fatal(err) |
| + } |
| + row, _ := fx.st.AccountPlan(acct.ID) |
| + if row.Throttle == nil || !row.Throttle.Bandwidth || row.Throttle.Awake || !row.Throttle.Until.Equal(graceEnd(capBandwidth, planNow)) { |
| + t.Fatalf("throttle = %+v", row.Throttle) |
| + } |
| + under := map[capID]capState{capBandwidth: monthRow(capBandwidth, 50, 100, planNow)} |
| + if err := fx.srv.convergeThrottle(ctx, acct, under, map[capID]bool{capBandwidth: true}, both(true), planNow.Add(time.Hour)); err != nil { |
| + t.Fatal(err) |
| + } |
| + if row, _ := fx.st.AccountPlan(acct.ID); row.Throttle != nil { |
| + t.Fatalf("throttle after falling under = %+v", row.Throttle) |
| + } |
| + events, _ := fx.st.Events(acct.ID, "", 20) |
| + var actions []string |
| + for _, e := range events { |
| + actions = append(actions, e.Action) |
| + } |
| + joined := strings.Join(actions, ",") |
| + if !strings.Contains(joined, "plan.throttled") || !strings.Contains(joined, "plan.unthrottled") { |
| + t.Errorf("audit = %v, want plan.throttled and plan.unthrottled", actions) |
| + } |
| +} |
| + |
| +func TestThrottleUnchangedWhenUnmeasured(t *testing.T) { |
| + fx, acct := throttleFixture(t) |
| + ctx := context.Background() |
| + states := map[capID]capState{capBandwidth: monthRow(capBandwidth, 200, 100, planNow)} |
| + if err := fx.srv.convergeThrottle(ctx, acct, states, map[capID]bool{capBandwidth: true}, both(true), planNow); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := fx.srv.convergeThrottle(ctx, acct, map[capID]capState{}, both(false), both(true), planNow.Add(time.Hour)); err != nil { |
| + t.Fatal(err) |
| + } |
| + if row, _ := fx.st.AccountPlan(acct.ID); row.Throttle == nil || !row.Throttle.Bandwidth { |
| + t.Fatalf("an unmeasured pass changed the throttle: %+v", row.Throttle) |
| + } |
| + // A plan without the cap clears it. |
| + if err := fx.srv.convergeThrottle(ctx, acct, map[capID]capState{}, both(false), both(false), planNow.Add(2*time.Hour)); err != nil { |
| + t.Fatal(err) |
| + } |
| + if row, _ := fx.st.AccountPlan(acct.ID); row.Throttle != nil { |
| + t.Fatalf("a plan without the caps kept the throttle: %+v", row.Throttle) |
| + } |
| +} |
| + |
| +func TestThrottleWithoutACatalogUnchanged(t *testing.T) { |
| + fx, acct := throttleFixture(t) |
| + fx.srv.cfg.PlanCatalog = nil |
| + states := map[capID]capState{capBandwidth: monthRow(capBandwidth, 200, 100, planNow)} |
| + if err := fx.srv.convergeThrottle(context.Background(), acct, states, map[capID]bool{capBandwidth: true}, both(true), planNow); err != nil { |
| + t.Fatal(err) |
| + } |
| + if row, _ := fx.st.AccountPlan(acct.ID); row.Throttle != nil { |
| + t.Fatal("throttled with plans off") |
| + } |
| +} |