wasm

package
v0.7.23 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 23, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package wasm is the in-memory warm-worker pool for WASM sandboxes (Phase 5). Unlike internal/pool/vmm, slots are not persisted — worker processes do not survive a daemon restart.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoSlot = errors.New("wasm warm pool: no slot available")

ErrNoSlot means the warm pool has no ready worker for the requested module digest.

Functions

func NewSlotID

func NewSlotID() string

NewSlotID mints a unique pool slot identifier.

func RunRefill

func RunRefill(ctx context.Context, pool *Pool, cfg RefillConfig, spawner Spawner, logger *slog.Logger)

RunRefill is a convenience wrapper matching daemon wiring style.

Types

type Metrics

type Metrics struct {
	Hits      atomic.Int64
	Misses    atomic.Int64
	Refilled  atomic.Int64
	SpawnFail atomic.Int64
}

Metrics tracks warm-pool hit/miss and refill counters (expvar-friendly via Snapshot).

func (*Metrics) RecordRefill

func (m *Metrics) RecordRefill()

func (*Metrics) RecordSpawnFail

func (m *Metrics) RecordSpawnFail()

func (*Metrics) Stats

func (m *Metrics) Stats() Snapshot

Stats returns current counter values.

type Pool

type Pool struct {
	// contains filtered or unexported fields
}

Pool keeps pre-loaded workers keyed by module digest.

func New

func New(runDir string, logger *slog.Logger) *Pool

New constructs a warm pool. runDir is the WASM runtime root (pool slots live under runDir/pool/).

func (*Pool) Acquire

func (p *Pool) Acquire(_ context.Context, digest, modulePath string, memoryMB int) (*Slot, error)

Acquire removes one warm slot for digest whose MemoryMB matches. Returns ErrNoSlot on miss.

func (*Pool) Close

func (p *Pool) Close() (drained int)

Close shuts down all warm slots still in the pool.

func (*Pool) DepthFor

func (p *Pool) DepthFor(digest string) int

DepthFor returns the target warm depth for digest.

func (*Pool) DropModule

func (p *Pool) DropModule(digest string)

DropModule evicts warm slots and refill targets for digest after module GC.

func (*Pool) ListTargets

func (p *Pool) ListTargets() []struct{ Digest, Path string }

ListTargets returns digest/path pairs eligible for refill.

func (*Pool) MarkSpawning

func (p *Pool) MarkSpawning(digest string)

MarkSpawning increments the in-flight spawn counter for digest.

func (*Pool) Metrics

func (p *Pool) Metrics() *Metrics

Metrics returns the pool's counter bundle.

func (*Pool) NoteModule

func (p *Pool) NoteModule(digest, modulePath string)

NoteModule registers a digest/path pair for background refill.

func (*Pool) ReadyCount

func (p *Pool) ReadyCount(digest string) int

ReadyCount returns how many warm slots are queued for digest.

func (*Pool) RecordLoaded

func (p *Pool) RecordLoaded(slot *Slot)

RecordLoaded pushes a freshly warmed slot into the ready queue.

func (*Pool) Run

func (p *Pool) Run(ctx context.Context, cfg RefillConfig, spawner Spawner)

Run starts the refill loop until ctx is cancelled. No-op when pool or spawner is nil.

func (*Pool) SetDefaultDepth

func (p *Pool) SetDefaultDepth(n int)

SetDefaultDepth sets the target warm depth per module digest when depth is unset.

func (*Pool) SetDefaultMemoryMB

func (p *Pool) SetDefaultMemoryMB(n int)

SetDefaultMemoryMB sets the guest memory cap used when warming pool slots.

func (*Pool) SetSpawner

func (p *Pool) SetSpawner(s Spawner)

SetSpawner injects the worker spawner (production: SupervisorSpawner).

func (*Pool) SlotDir

func (p *Pool) SlotDir(digest, slotID string) string

SlotDir returns the on-disk directory for a pool slot.

func (*Pool) SpawnBudget

func (p *Pool) SpawnBudget(digest string) int

SpawnBudget returns how many slots refill should create for digest on this tick.

func (*Pool) UnmarkSpawning

func (p *Pool) UnmarkSpawning(digest string)

UnmarkSpawning decrements the in-flight spawn counter after a failed warm.

func (*Pool) WarmOne

func (p *Pool) WarmOne(ctx context.Context, digest, modulePath string) (*Slot, error)

WarmOne spawns a single warm slot for digest/path. Used by the refill loop.

type RefillConfig

type RefillConfig struct {
	RefillInterval time.Duration
	SpawnTimeout   time.Duration
}

RefillConfig holds timing knobs for the background refill loop.

type Slot

type Slot struct {
	ID           string
	ModuleDigest string
	ModulePath   string
	SocketPath   string
	WorkerKey    string
	MemoryMB     int
	LoadedAt     time.Time
}

Slot is one warm worker with its module already compiled inside the worker.

func AcquireOrMiss

func AcquireOrMiss(ctx context.Context, p *Pool, digest, path string, memoryMB int) (*Slot, bool, error)

AcquireOrMiss is a helper that normalizes ErrNoSlot for callers that want a bool.

type Snapshot

type Snapshot struct {
	Hits      int64
	Misses    int64
	Refilled  int64
	SpawnFail int64
}

Snapshot returns a point-in-time view of pool counters.

type Spawner

type Spawner interface {
	Warm(ctx context.Context, slotID, socketPath, modulePath string, memoryMB int) error
	Shutdown(slotID string) error
}

Spawner warms a worker subprocess with a module already compiled (LoadModule done).

type SupervisorSpawner

type SupervisorSpawner struct {
	Supervisor *worker.Supervisor
	PingWait   time.Duration
	MemoryMB   int
}

SupervisorSpawner uses the shared worker.Supervisor to own pool slot processes.

func NewSupervisorSpawner

func NewSupervisorSpawner(sup *worker.Supervisor) *SupervisorSpawner

NewSupervisorSpawner constructs a spawner backed by worker.Supervisor.

func (*SupervisorSpawner) Shutdown

func (s *SupervisorSpawner) Shutdown(slotID string) error

Shutdown stops the worker subprocess for slotID.

func (*SupervisorSpawner) Warm

func (s *SupervisorSpawner) Warm(ctx context.Context, slotID, socketPath, modulePath string, memoryMB int) error

Warm starts (or reuses) a worker at socketPath and loads modulePath.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL