wasmkind

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: Apache-2.0, MIT Imports: 19 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package wasmkind runs the kinds of request of a requests directory that holds them as Wasm: the module ritsu builds from the project's Rust package (kinds.wasm), and how to call it (wasm.json), both written by `ritsu gen --target requests --extra wasm`. Load answers the package a server takes, as server.Load does for a generated Go package, so that a server can run a new version of a kind, or a new kind, without being built again.

What runs is the code ritsu generated, as in a Go package: each entry gives the module's door one line of JSON, as the Rust package's list of kinds does, and answers what the door answers, to the character. The module is compiled once, by wazero (no cgo), and runs in instances of its own memory, which import nothing: a module that imports anything is refused.

Each call runs in a new instance, within limits: a time (Options.Time), past which it is stopped, and a number of pages its instance's memory may grow to (Options.Pages). A call stopped by either, or by a trap of the module, is answered with a *StopError that says which (ErrTime, ErrMemory, ErrTrap). Nothing of it is left for the next call. A call that finds no instance free within the same time is not run, and is answered with ErrBusy.

Index

Constants

View Source
const (
	// DefaultTime is how long one call of an entry may run.
	DefaultTime = 2 * time.Second
	// DefaultPages is how many pages of 64 KiB an instance's memory may grow to: 64 MiB.
	DefaultPages = 1024
	// DefaultStartPages is how many pages of 64 KiB a module's memory may start with: 4 MiB. It
	// holds the heap every call pays for, whether the call uses it or not.
	DefaultStartPages = 64
)

The limits a call runs within, where Options gives none.

Variables

View Source
var (
	// ErrTime is a call that ran past its time (Options.Time).
	ErrTime = errors.New("the call ran past its time")
	// ErrMemory is a call that grew its instance's memory past its pages (Options.Pages).
	ErrMemory = errors.New("the call grew its memory past its pages")
	// ErrTrap is a call the module stopped with a trap of WebAssembly (an unreachable, a division by
	// zero, a read outside its memory, its stack used up), that answered outside its memory, or
	// whose answer was not UTF-8.
	ErrTrap = errors.New("the module trapped")
	// ErrBusy is a call that found no instance free within its time (Options.Time), and was not
	// run. A server can answer it as one of too much work, and ask the caller to try again.
	ErrBusy = errors.New("no instance was free in time")
)

What stopped a call that answered nothing (StopError.Err).

View Source
var Versions = []int{1}

Versions are the versions of wasm.json this package reads.

Functions

func Load

func Load(ctx context.Context, requests fs.FS) (*server.Package, error)

Load reads a requests directory with wasm.json and kinds.wasm and answers its package.

func LoadWith

func LoadWith(ctx context.Context, requests fs.FS, o Options) (*server.Package, error)

LoadWith is Load with limits of its own.

It refuses a version of wasm.json it does not read, a module whose digest is not the one wasm.json says, a module that imports anything or lacks one of the exports wasm.json names (or has it of another type), and a list of kinds that is not the one of types/ and openapi.json (server.LoadKinds). The module is compiled once, here; ctx is that of the compiling, not of the calls.

Types

type KanameError

type KanameError struct {
	Kind    string
	Module  string
	Line    int
	Message string
	Path    string
}

KanameError is why a call of an entry is no call of the contract, as the generated Go package's KanameError says it: the kind of the failure (input where an argument is not what the function takes, as the door's refusals are too; rule, date, range, overflow or zero), the module and the line of the .kn file where it was found (none for a refusal), the sentence, in the package's language, and the place of what is not taken (`submit.form.days`), "" where the call is refused as a whole. The framework reads an entry's error of the kind input by these fields, of a type of this name, as it reads the Go package's (a sentence it answers a person with), and any other error as its own failure.

func (*KanameError) Error

func (e *KanameError) Error() string

type Options

type Options struct {
	// Time is how long one call of an entry may run (DefaultTime when 0). Waiting for a free
	// instance is within the same time: a call past it is stopped, or not run, and answered with
	// ErrTime or ErrBusy.
	Time time.Duration
	// Pages is how many pages of 64 KiB the memory of an instance may grow to (DefaultPages when
	// 0). A call that grows it past them is stopped, and answered with ErrMemory.
	Pages uint32
	// StartPages is how many pages of 64 KiB a module's memory may start with (DefaultStartPages
	// when 0, and never more than Pages). A module whose memory starts larger is refused.
	StartPages uint32
	// Instances is how many instances of the module run calls at once (two for each of the Go
	// scheduler's processors, runtime.GOMAXPROCS, when 0): a call past them waits for one to be
	// free, within Time. None is kept after its call.
	Instances int
	// Fresh is kept for source compatibility.
	//
	// Deprecated: every call runs in an instance of its own whether Fresh is set or not.
	Fresh bool
}

Options are the limits the calls of a module run within, and how many of its instances run them.

type StopError

type StopError struct {
	// Module and Function are the module and the function of the door the entry called.
	Module, Function string
	// Err is what stopped it: ErrTime, ErrMemory, ErrTrap or ErrBusy.
	Err error
	// Limit is the time or the pages the call ran past, as the sentence says it; "" for a trap.
	Limit string
	// Cause is what the runtime said, for a trap.
	Cause error
}

StopError is a call of an entry that was stopped: by its time (ErrTime), by its memory (ErrMemory), or by the module (ErrTrap); or one that was not run because no instance was free (ErrBusy). An instance a stopped call ran in is not used again.

func (*StopError) Busy

func (e *StopError) Busy() bool

Busy reports whether the stop is ErrBusy: too much work at once, which the one who called may retry after a breath, apart from a failure of the module. The server reads it through errors.As (server.Busy), as it does net.Error, so it does not import this package.

func (*StopError) Error

func (e *StopError) Error() string

func (*StopError) Unwrap

func (e *StopError) Unwrap() []error

Unwrap answers what stopped the call, and what the runtime said.

Jump to

Keyboard shortcuts

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