[Feature] SDK SandboxPool: dynamic / time-window-based pool sizing via a capacity-provider interface

Author: PangjipingCreated Sep 21, 2026Updated Sep 22, 2026

Background

The client-side SandboxPool (Go: sdks/sandbox/go/pool.go; JS: sdks/sandbox/javascript/src/pool.ts) keeps a warm buffer of clean, ready sandboxes so Acquire() stays fast. Today the pool size is effectively fixed at construction:

  • Go: SandboxPoolBuilder.MaxIdle(n), enforced by the internal reconcile loop. Changing it at runtime requires an explicit Resize(ctx, newMaxIdle) call per process.
  • JS: SandboxPoolOptions.maxIdle, fixed for the lifetime of the pool.

Problem

Real-world traffic is diurnal:

  • Daytime peak (e.g. 09:00–21:00): high QPS — a large warm pool is needed to keep acquisition latency low.
  • Nights / weekends: near-zero traffic, yet the pool still maintains the full maxIdle warm sandboxes, wasting compute and cost.

To survive peaks, users must set maxIdle to the peak value and leave it there 24/7. Resize() technically exists, but wiring it up means external glue (cron / CronJob calling Resize() on every client process), which is error-prone and multiplies across every service that owns a pool. This feels like a common, general-purpose need the SDK should support natively.

Proposal

Rather than hardcoding "time windows" into the SDK, introduce a small abstraction that the pool consults on every reconcile tick — users implement the interface to own the window logic (as discussed with maintainers), with a built-in time-window implementation covering the common case:

go
// Go sketch: PoolSizer returns the desired pool capacity for "now".
type PoolSizer interface {
    DesiredMaxIdle(ctx context.Context, snapshot PoolSnapshot) (int, error)
}
typescript
// JS sketch
type PoolSizer = (snapshot: PoolSnapshot) => number | Promise<number>;
  • SandboxPoolBuilder.Sizer(s) / SandboxPoolOptions.sizer: when set, the reconcile loop evaluates it each tick and treats the result as the effective maxIdle.
  • Ship a built-in TimeWindowSizer (list of windows, e.g. "workdays 09:00–21:00 => 50, otherwise => 5", with timezone support).
  • Default sizer = the static maxIdle value, so existing behavior is unchanged (backward compatible, additive API).

Semantics to nail down

  1. Scale-down: shrink gracefully — release surplus idle sandboxes only (respect min-TTL and in-flight acquires); never touch sandboxes already handed out.
  2. Scale-up: warm-up takes time; consider a "lead time" option (start warming before the window opens) or at least document convergence time.
  3. Flapping protection: hysteresis / cooldown between resizes to avoid churn on boundary times.
  4. Observability: PoolSnapshot should expose the current effective target size (and which sizer produced it), plus logs on resize events.
  5. Multi-process pools: with a shared stateStore, the target must be consistent across processes (e.g. only the primary evaluates the sizer, or the target is derived from shared state).
  6. Lifecycle interaction: sizer output must not resurrect a pool fenced by SandboxPoolManager.Destroy (DESTROYING fence / tombstone).

Alternatives considered

  • External cron calling Resize() per process: works today, but requires per-process orchestration and custom cross-process coordination; easy to get wrong.
  • Server / K8s-side scheduling: a different layer (Pool CRD / controller); the client pool is where the idle cost is actually incurred in this scenario.

Questions

  • Do maintainers prefer the user-implemented PoolSizer interface (time windows = one built-in impl), or a first-class declarative schedule option on the pool?
  • Should this land in Go first and then align with JS and other language SDKs?

Source: opensandbox-group/OpenSandbox