activator: a per-route continuous-awake ceiling for instances (plans 4c)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+100
−0
internal/activator/activator.go+27 −0internal/activator/activator_maxrunfor_test.go+72 −0internal/host/agent.go+1 −0
diff --git a/internal/activator/activator.go b/internal/activator/activator.go| index 3c6870a57..97d513ce9 100644 |
| --- a/internal/activator/activator.go |
| +++ b/internal/activator/activator.go |
| @@ -1143,6 +1143,10 @@ type Service struct { |
| // entry's route it names the plan's idle window, or 0 for no opinion. It |
| // can only shorten a window. nil means today's behaviour. |
| idleFor atomic.Pointer[func(registry.Route) time.Duration] |
| + // maxRunFor is the plans continuous-awake hook (plans 4c): a ceiling on |
| + // how long an instance stays awake, busy or not; 0 is none. Workers |
| + // keep their own MaxRunSeconds and are never asked. |
| + maxRunFor atomic.Pointer[func(registry.Route) time.Duration] |
| cfg Config |
| backend Backend |
| @@ -4722,6 +4726,18 @@ func (a *Service) sweepOnce() { |
| a.beginStopLocked(e, StopIdle) |
| continue |
| } |
| + // The plans continuous-awake ceiling (plans 4c, spec §5.2): an |
| + // awake-throttled account's instance that is never idle (a steady |
| + // pinger, a long stream) still sleeps after the ceiling, whatever is |
| + // in flight, and wakes on its next request. Never a worker, and |
| + // never mid-command. |
| + if f := a.maxRunFor.Load(); f != nil && !e.worker && hibernates && e.cmd == nil { |
| + if d := (*f)(e.route); d > 0 && now.Sub(e.spawnedAt) >= d { |
| + log.Printf("activator: %s awake %s, past its plan's ceiling of %s — stopping", e.host, now.Sub(e.spawnedAt).Truncate(time.Second), d) |
| + a.beginStopLocked(e, StopIdle) |
| + continue |
| + } |
| + } |
| // Converge onto a shipped build. A running child never re-reads |
| // live/<host>, so once a ship repoints the symlink this awake child is |
| // serving a stale binary. Idle hosts converge for free on their next |
| @@ -5489,3 +5505,14 @@ func (a *Service) SetIdleFor(f func(registry.Route) time.Duration) { |
| } |
| a.idleFor.Store(&f) |
| } |
| + |
| +// SetMaxRunFor installs the plans continuous-awake hook (plans 4c): an |
| +// instance awake for that long is put to sleep even while busy, and wakes |
| +// on its next request. nil, and a return of 0, mean no ceiling. |
| +func (a *Service) SetMaxRunFor(f func(registry.Route) time.Duration) { |
| + if f == nil { |
| + a.maxRunFor.Store(nil) |
| + return |
| + } |
| + a.maxRunFor.Store(&f) |
| +} |
diff --git a/internal/activator/activator_maxrunfor_test.go b/internal/activator/activator_maxrunfor_test.go| new file mode 100644 |
| index 000000000..fe4469083 |
| --- /dev/null |
| +++ b/internal/activator/activator_maxrunfor_test.go |
| @@ -0,0 +1,72 @@ |
| +package activator |
| + |
| +import ( |
| + "context" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/registry" |
| +) |
| + |
| +// The plans continuous-awake hook (plans 4c): an awake-throttled account's |
| +// instance sleeps after 15 minutes awake even while busy, and wakes on its |
| +// next request. |
| + |
| +func maxRunFor(d time.Duration) func(registry.Route) time.Duration { |
| + return func(registry.Route) time.Duration { return d } |
| +} |
| + |
| +func TestMaxRunForStopsABusyInstance(t *testing.T) { |
| + t.Parallel() |
| + a, _, clk, _ := testActivator(t, 15*time.Minute) |
| + a.SetMaxRunFor(maxRunFor(15 * time.Minute)) |
| + release, err := a.beginRequest(context.Background(), testRoute("one.example.com")) |
| + if err != nil { |
| + t.Fatalf("beginRequest: %v", err) |
| + } |
| + defer release() // held in flight throughout |
| + clk.advance(14 * time.Minute) |
| + a.sweepOnce() |
| + if a.entryCount() != 1 { |
| + t.Fatal("stopped before the 15-minute ceiling") |
| + } |
| + clk.advance(time.Minute + time.Second) |
| + a.sweepOnce() |
| + waitFor(t, "ceiling stop of a busy instance", func() bool { return a.entryCount() == 0 }) |
| + wakeInstance(t, a, "one.example.com") |
| + if a.entryCount() != 1 { |
| + t.Fatal("the next request did not wake it") |
| + } |
| +} |
| + |
| +func TestMaxRunForZeroUnchanged(t *testing.T) { |
| + t.Parallel() |
| + a, _, clk, _ := testActivator(t, 15*time.Minute) |
| + a.SetMaxRunFor(maxRunFor(0)) |
| + release, err := a.beginRequest(context.Background(), testRoute("one.example.com")) |
| + if err != nil { |
| + t.Fatalf("beginRequest: %v", err) |
| + } |
| + defer release() |
| + clk.advance(2 * time.Hour) |
| + a.sweepOnce() |
| + if a.entryCount() != 1 { |
| + t.Fatal("a zero ceiling stopped a busy instance") |
| + } |
| +} |
| + |
| +func TestMaxRunForLeavesWorkersAlone(t *testing.T) { |
| + t.Parallel() |
| + a, _, clk, _ := testActivator(t, 15*time.Minute) |
| + a.SetMaxRunFor(maxRunFor(time.Minute)) |
| + rt := testWorkerRoute("bot.x.internal") |
| + rt.Hibernate = false // always on: no idle clock, so only a ceiling could stop it |
| + if err := a.WakeWorker(rt); err != nil { |
| + t.Fatalf("WakeWorker: %v", err) |
| + } |
| + clk.advance(5 * time.Minute) |
| + a.sweepOnce() |
| + if !a.entryAwake("bot.x.internal") { |
| + t.Fatal("the instance ceiling stopped a worker") |
| + } |
| +} |
diff --git a/internal/host/agent.go b/internal/host/agent.go| index d07a9acfd..d6bf2979e 100644 |
| --- a/internal/host/agent.go |
| +++ b/internal/host/agent.go |
| @@ -500,6 +500,7 @@ func runAgent(args []string, buildClaims ClaimFactory) { |
| // plans off (no marker, CARLOS_PLAN_LIMITS=off, or no record) the hook |
| // answers 0 and every window is the box's own. |
| svc.SetIdleFor(planIdleFor) |
| + svc.SetMaxRunFor(planMaxRunFor) // plans 4c: the continuous-awake ceiling |
| // Settings spec §4.2: before any process runs a build against an |
| // instance's database, the build state names it, under the lease. A |
| // store that failed to start refuses every spawn (never "run |