Documentation
¶
Overview ¶
Package worker implements the WASM worker-subprocess pool (plans/wasm-runtime.md §2.1). Phase 1 lands length-prefixed JSON framing; CBOR encoding can replace the codec without changing the message types.
Index ¶
- func DefaultResidentSpawner(ctx context.Context, socketPath string) (*exec.Cmd, error)
- func DefaultSpawner(ctx context.Context, socketPath string) (*exec.Cmd, error)
- func ReadyPollSleep(ctx context.Context, attempt int)
- func RunCLI(args []string) error
- func RunCLIResident(args []string) error
- func ServeSocketPath(socketPath string) error
- func ServeSocketPathResident(socketPath string) error
- type Client
- func (c *Client) Checkpoint(ctx context.Context, sandboxID, outDir string, meta wasmengine.SnapshotConfig) error
- func (c *Client) Exec(sandboxID string, caps wasmengine.Capabilities, export string) (wasmengine.RunResult, error)
- func (c *Client) InstanceLoaded(ctx context.Context, sandboxID string) (bool, error)
- func (c *Client) Instantiate(sandboxID string, caps wasmengine.Capabilities) error
- func (c *Client) Invoke(sandboxID, export string) error
- func (c *Client) LoadModule(sandboxID, path string, memoryMB int) (wasmengine.LoadTimings, error)
- func (c *Client) NetstatsTick(sandboxID string) (bytesIn, bytesOut int64, err error)
- func (c *Client) Ping(sandboxID string) error
- func (c *Client) ProxyHTTP(sandboxID string, guestPort int, w http.ResponseWriter, r *http.Request) error
- func (c *Client) ResolvedListenPort(sandboxID string) (int, error)
- func (c *Client) Restore(sandboxID, dir string, caps wasmengine.Capabilities) error
- func (c *Client) SetCapability(sandboxID string, caps wasmengine.Capabilities) error
- func (c *Client) SetListenPort(sandboxID string, port int, host string) error
- func (c *Client) SetNetworkBlocks(sandboxID string, blockIngress, blockEgress bool) error
- func (c *Client) StopInstance(sandboxID string) error
- func (c *Client) TriggerPanic(sandboxID string) error
- type Envelope
- type MessageType
- type NetMediator
- func (m *NetMediator) Copy(sandboxID string, dst io.Writer, src io.Reader, outbound bool) (int64, error)
- func (m *NetMediator) DialContext(ctx context.Context, sandboxID, network, address string) (net.Conn, error)
- func (m *NetMediator) DrainUsage(sandboxID string) (in, out int64)
- func (m *NetMediator) SetBlocks(sandboxID string, blockIngress, blockEgress bool)
- type ResidentServer
- type Server
- type Slot
- type Spawner
- type Supervisor
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefaultResidentSpawner ¶
DefaultResidentSpawner runs the current executable as a resident-module host (--wasm-resident-host → RunCLIResident): one process compiles a module once and hosts many isolated sandbox instances. Used by the driver's resident-host supervisor when SB_WASM_RESIDENT_HOST_ENABLED is set.
func DefaultSpawner ¶
DefaultSpawner runs the current executable in worker mode. Requires main to delegate --wasm-worker to worker.RunCLI (wired from cmd/sandboxd in Phase 1).
func ReadyPollSleep ¶
ReadyPollSleep waits before the next worker readiness ping. attempt 0 is a no-op (first ping fires immediately); subsequent attempts use 2ms doubling to a 25ms cap (plans/wasm-create-latency.md Phase 4).
func RunCLIResident ¶
RunCLIResident is the `--wasm-resident-host <socket>` entrypoint.
func ServeSocketPath ¶
ServeSocketPath listens on a Unix domain socket and serves each connection concurrently.
func ServeSocketPathResident ¶
ServeSocketPathResident listens on a Unix socket and serves each connection with a shared ResidentServer (one MultiInstanceEngine per process).
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client talks to one worker over a Unix domain socket.
func (*Client) Checkpoint ¶
func (c *Client) Checkpoint(ctx context.Context, sandboxID, outDir string, meta wasmengine.SnapshotConfig) error
Checkpoint writes a mem.snap artifact to outDir.
func (*Client) Exec ¶
func (c *Client) Exec(sandboxID string, caps wasmengine.Capabilities, export string) (wasmengine.RunResult, error)
Exec re-instantiates with caps, invokes export, and returns captured IO.
func (*Client) InstanceLoaded ¶
InstanceLoaded reports whether the worker currently has an engine/module loaded.
func (*Client) Instantiate ¶
func (c *Client) Instantiate(sandboxID string, caps wasmengine.Capabilities) error
Instantiate creates a WASI instance with the given capabilities.
func (*Client) LoadModule ¶
func (c *Client) LoadModule(sandboxID, path string, memoryMB int) (wasmengine.LoadTimings, error)
LoadModule compiles the module at path inside the worker process and returns the sub-stage timing breakdown (best-effort; zero-valued if the worker did not report it) so the host create path can emit wasm_load Server-Timing subs.
func (*Client) NetstatsTick ¶
NetstatsTick drains worker-side byte counters since the last poll (UC-43).
func (*Client) ProxyHTTP ¶
func (c *Client) ProxyHTTP(sandboxID string, guestPort int, w http.ResponseWriter, r *http.Request) error
ProxyHTTP forwards one HTTP request to the guest wasip1 listener inside the worker.
func (*Client) ResolvedListenPort ¶
ResolvedListenPort returns the host port for the active wasip1 listener (after ephemeral bind).
func (*Client) Restore ¶
func (c *Client) Restore(sandboxID, dir string, caps wasmengine.Capabilities) error
Restore loads a mem.snap artifact from dir into the active worker engine.
func (*Client) SetCapability ¶
func (c *Client) SetCapability(sandboxID string, caps wasmengine.Capabilities) error
SetCapability hot-updates memory/wall-timeout caps on a running instance.
func (*Client) SetListenPort ¶
SetListenPort hot-updates the wasip1 listener port without touching memory caps.
func (*Client) SetNetworkBlocks ¶
SetNetworkBlocks applies quota blocks at the worker-side socket mediator (UC-43).
func (*Client) StopInstance ¶
StopInstance tears down the active instance inside the worker.
func (*Client) TriggerPanic ¶
TriggerPanic sends the test-only panic message. The worker process is expected to exit.
type Envelope ¶
type Envelope struct {
Type MessageType `json:"type"`
SandboxID string `json:"sandbox_id,omitempty"`
Payload []byte `json:"payload,omitempty"`
}
Envelope is the on-wire message body (after the length prefix).
type MessageType ¶
type MessageType string
MessageType identifies a worker control-plane message.
const ( MsgHealthPing MessageType = "health_ping" MsgPong MessageType = "pong" MsgInstanceStatus MessageType = "instance_status" MsgTriggerPanic MessageType = "trigger_panic" // test-only: verifies crash isolation (D10) MsgLoadModule MessageType = "load_module" MsgInstantiate MessageType = "instantiate" MsgInvoke MessageType = "invoke" MsgExec MessageType = "exec" MsgStopInstance MessageType = "stop_instance" MsgOK MessageType = "ok" MsgError MessageType = "error" MsgInvokeResult MessageType = "invoke_result" MsgCheckpoint MessageType = "checkpoint" MsgRestore MessageType = "restore" MsgSetCapability MessageType = "set_capability" MsgNetstatsTick MessageType = "netstats_tick" MsgSetNetworkBlocks MessageType = "set_network_blocks" MsgSetListenPort MessageType = "set_listen_port" MsgListenPort MessageType = "listen_port" MsgProxyHTTP MessageType = "proxy_http" MsgProxyHTTPResult MessageType = "proxy_http_result" )
type NetMediator ¶
type NetMediator struct {
// contains filtered or unexported fields
}
NetMediator is the host-mediated TCP egress surface for UC-43. Guest WASI sockets (when wired) and host-side proxies dial through here so bytes and quota blocks are observable per sandbox.
func (*NetMediator) Copy ¶
func (m *NetMediator) Copy(sandboxID string, dst io.Writer, src io.Reader, outbound bool) (int64, error)
Copy counts bytes moved through the mediator for tests and host proxies.
func (*NetMediator) DialContext ¶
func (m *NetMediator) DialContext(ctx context.Context, sandboxID, network, address string) (net.Conn, error)
DialContext dials address when egress is allowed and counts bytes in/out.
func (*NetMediator) DrainUsage ¶
func (m *NetMediator) DrainUsage(sandboxID string) (in, out int64)
DrainUsage returns and clears accumulated socket bytes for sandboxID.
func (*NetMediator) SetBlocks ¶
func (m *NetMediator) SetBlocks(sandboxID string, blockIngress, blockEgress bool)
type ResidentServer ¶
type ResidentServer struct {
// contains filtered or unexported fields
}
ResidentServer serves the worker protocol backed by a MultiInstanceEngine: one resident process compiles a module once and hosts many isolated sandbox instances (compile-once, instantiate-many). Spawned via `--wasm-resident-host`.
Phase 2b PR-A: per-instance egress via MultiInstanceEngine.SetNetworkHook (name-keyed hooks + conn ownership) and a shared NetMediator. Listener (expose_port/HTTP) and checkpoint/restore remain rejected — those stay on the per-sandbox cold path (migrate-on-expose is PR-B).
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server holds one wazero engine per worker process (D11: one module per worker).
type Spawner ¶
Spawner starts a worker subprocess bound to socketPath. Production wiring uses `sandboxd --wasm-worker <socket>`; tests inject a subprocess of this test binary.
type Supervisor ¶
type Supervisor struct {
// contains filtered or unexported fields
}
Supervisor owns worker subprocesses for WASM sandboxes. A worker crash kills only that subprocess; the supervisor respawns the slot while sandboxd keeps running.
func NewSupervisor ¶
func NewSupervisor(spawn Spawner) *Supervisor
NewSupervisor constructs a supervisor. spawn must be non-nil.
func (*Supervisor) Ensure ¶
func (s *Supervisor) Ensure(ctx context.Context, sandboxID, socketPath string) error
Ensure starts (or restarts) the worker for sandboxID at socketPath.
func (*Supervisor) SpawnCount ¶
func (s *Supervisor) SpawnCount(sandboxID string) int
SpawnCount returns how many times the worker for sandboxID has been started.
func (*Supervisor) Stop ¶
func (s *Supervisor) Stop(sandboxID string) error
Stop terminates the worker for sandboxID.