plans: turned-off hosts in the limits record, and what counts as a fall (plans 4d)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+75
−1
internal/plans/lapse.go+19 −0internal/plans/lapse_test.go+28 −0internal/plans/record.go+4 −0internal/plans/record_test.go+24 −1
diff --git a/internal/plans/lapse.go b/internal/plans/lapse.go| new file mode 100644 |
| index 000000000..0d3b26578 |
| --- /dev/null |
| +++ b/internal/plans/lapse.go |
| @@ -0,0 +1,19 @@ |
| +package plans |
| + |
| +// LimitsFell reports whether next is lower than old in a limit the lapse |
| +// lifecycle governs (plans 4d): running things, workers per app, custom |
| +// domains, fleets and the schedule minimum. Other limits have their own |
| +// stages (plans 4b, 4c) and never lead to anything being turned off. A zero |
| +// count is "not set", which is unlimited. |
| +func LimitsFell(old, next Limits) bool { |
| + countFell := func(o, n int) bool { return n > 0 && (o == 0 || n < o) } |
| + switch { |
| + case countFell(old.RunningThings, next.RunningThings), |
| + countFell(old.SidecarsPerApp, next.SidecarsPerApp), |
| + old.CustomDomains && !next.CustomDomains, |
| + old.Fleets && !next.Fleets, |
| + next.MinScheduleSeconds > old.MinScheduleSeconds: |
| + return true |
| + } |
| + return false |
| +} |
diff --git a/internal/plans/lapse_test.go b/internal/plans/lapse_test.go| new file mode 100644 |
| index 000000000..dcff8eddd |
| --- /dev/null |
| +++ b/internal/plans/lapse_test.go |
| @@ -0,0 +1,28 @@ |
| +package plans |
| + |
| +import "testing" |
| + |
| +func TestLimitsFell(t *testing.T) { |
| + cases := []struct { |
| + name string |
| + old, next Limits |
| + want bool |
| + }{ |
| + {"running 25->3", Limits{RunningThings: 25}, Limits{RunningThings: 3}, true}, |
| + {"running 3->25", Limits{RunningThings: 3}, Limits{RunningThings: 25}, false}, |
| + {"running unlimited->3", Limits{}, Limits{RunningThings: 3}, true}, |
| + {"running 3->unlimited", Limits{RunningThings: 3}, Limits{}, false}, |
| + {"sidecars 5->1", Limits{SidecarsPerApp: 5}, Limits{SidecarsPerApp: 1}, true}, |
| + {"domains lost", Limits{CustomDomains: true}, Limits{}, true}, |
| + {"fleets lost", Limits{Fleets: true}, Limits{}, true}, |
| + {"min schedule 60->900", Limits{MinScheduleSeconds: 60}, Limits{MinScheduleSeconds: 900}, true}, |
| + {"min schedule 900->60", Limits{MinScheduleSeconds: 900}, Limits{MinScheduleSeconds: 60}, false}, |
| + {"storage alone", Limits{StorageBytes: 25 << 30}, Limits{StorageBytes: 1 << 30}, false}, |
| + {"identical", Limits{RunningThings: 3, CustomDomains: true}, Limits{RunningThings: 3, CustomDomains: true}, false}, |
| + } |
| + for _, c := range cases { |
| + if got := LimitsFell(c.old, c.next); got != c.want { |
| + t.Errorf("%s: LimitsFell = %v, want %v", c.name, got, c.want) |
| + } |
| + } |
| +} |
diff --git a/internal/plans/record.go b/internal/plans/record.go| index e5077ba7e..903b30c10 100644 |
| --- a/internal/plans/record.go |
| +++ b/internal/plans/record.go |
| @@ -32,6 +32,10 @@ type Record struct { |
| // Throttle, when set, slows the account for the rest of the month |
| // (plans 4c). |
| Throttle *Throttle `json:"throttle,omitempty"` |
| + // Disabled lists hosts turned off after a lapse (plans 4d): instance and |
| + // sidecar hosts, custom domains, and "*.<parent>" for a catch-all. Sorted, |
| + // lower case. Boxes answer them with the disabled page and never wake them. |
| + Disabled []string `json:"disabled,omitempty"` |
| } |
| // LimitsKey is where an account's limits record lives. |
diff --git a/internal/plans/record_test.go b/internal/plans/record_test.go| index 669f03252..986557d6c 100644 |
| --- a/internal/plans/record_test.go |
| +++ b/internal/plans/record_test.go |
| @@ -3,6 +3,7 @@ package plans |
| import ( |
| "context" |
| "errors" |
| + "reflect" |
| "testing" |
| "time" |
| @@ -45,11 +46,33 @@ func TestRecordRoundTrip(t *testing.T) { |
| t.Fatalf("etag %q, schema %d", getEtag, got.Schema) |
| } |
| want.Schema = RecordSchema |
| - if *got != *want { |
| + if !reflect.DeepEqual(got, want) { |
| t.Fatalf("got %+v, want %+v", got, want) |
| } |
| } |
| +func TestRecordRoundTripsDisabled(t *testing.T) { |
| + ctx := context.Background() |
| + s := release.NewDirStore(t.TempDir()) |
| + sqid := domains.Sqid(42) |
| + want := sampleRecord(sqid) |
| + want.Disabled = []string{"*.example.com", "shop.example.com"} |
| + if _, err := PutLimits(ctx, s, want, ""); err != nil { |
| + t.Fatal(err) |
| + } |
| + got, _, err := GetLimits(ctx, s, sqid) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if !reflect.DeepEqual(got.Disabled, want.Disabled) { |
| + t.Fatalf("Disabled = %v, want %v", got.Disabled, want.Disabled) |
| + } |
| + r, err := ParseLimits([]byte(`{"schema": 1, "account": "abc", "plan": "free"}`), "abc") |
| + if err != nil || r.Disabled != nil { |
| + t.Fatalf("record without disabled: %+v, %v", r, err) |
| + } |
| +} |
| + |
| func TestGetRecordAbsent(t *testing.T) { |
| got, etag, err := GetLimits(context.Background(), release.NewDirStore(t.TempDir()), domains.Sqid(42)) |
| if got != nil || etag != "" || err != nil { |