engine

package
v0.1.105 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package engine runs pack WASM modules in wazero: a module compiled once, a bounded pool of pre-warmed instances, and calls with a deadline that pass JSON through linear memory (the ABI in core/src/abi.rs).

Index

Constants

This section is empty.

Variables

View Source
var ErrPoolBusy = errors.New("engine: every instance is busy")

ErrPoolBusy is returned when no instance frees up before the deadline.

Functions

func CallJSON

func CallJSON(ctx context.Context, p *Pool, fn string, in, out any) error

CallJSON encodes in, calls fn and decodes into out.

func ErrorCode

func ErrorCode(err error) string

ErrorCode returns the code of the capability error in err's chain, or "" when there is none:

out, err := media.From(ctx).ImageResize(ctx, in)
if engine.ErrorCode(err) == "unsupported_format" { /* keep the original */ }

Types

type CallError

type CallError struct {
	Capability string
	Message    string
	// Code names the kind of failure when the capability gave one
	// (abi::Error::code in Rust), such as "unsupported_format"; empty
	// otherwise.
	Code string
}

CallError is an error the capability itself returned.

func (*CallError) Error

func (e *CallError) Error() string

type Module

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

Module is a compiled WASM module.

func Compile

func Compile(ctx context.Context, wasm []byte, opt Options) (*Module, error)

Compile compiles wasm with WASI available to it and no filesystem, clock or network beyond what WASI preview 1 exposes.

func (*Module) Close

func (m *Module) Close(ctx context.Context) error

Close releases the runtime and every instance made from the module.

func (*Module) Exports

func (m *Module) Exports() []string

Exports lists the exported function names.

type Options

type Options struct {
	// MemoryMB caps each instance's linear memory; default 64.
	MemoryMB int
	// Uninterruptible drops the deadline checks the compiler otherwise
	// inserts at every loop back-edge. Loops run several times faster; a
	// call that overruns the pool deadline then returns an error to its
	// caller but keeps running inside its instance until it finishes, and
	// that instance is discarded afterwards. For capabilities whose loops
	// are bounded by their input.
	Uninterruptible bool
}

Options bounds a module.

type Pool

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

Pool is a bounded set of pre-warmed instances. A call takes one, runs with the pool's deadline, and returns it; an instance whose call was cut off by the deadline is discarded and replaced.

func NewPool

func NewPool(ctx context.Context, m *Module, size int, timeout time.Duration) (*Pool, error)

NewPool instantiates size instances up front. timeout bounds every call.

func (*Pool) Call

func (p *Pool) Call(ctx context.Context, fn string, input []byte) ([]byte, error)

Call runs the capability fn with a JSON input and returns its JSON output. It waits for an instance no longer than the pool timeout.

func (*Pool) Close

func (p *Pool) Close(ctx context.Context) error

Close closes every idle instance; in-flight calls finish first up to the timeout.

func (*Pool) Stats

func (p *Pool) Stats() map[string]float64

Stats reports counters and the pool state for /metrics.

Jump to

Keyboard shortcuts

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