wasm_tools_call (wasm_tools v0.1.0)

View Source

One call of a component function: a fresh instance in a process that can be killed, with a deadline, a cancel and an observer.

This module is internal: you reach it through wasm_tools, and it can change in any release. The README lists the supported API.

An instance belongs to the process that made it, and a call that runs inline can only be stopped by killing that process. So every call runs in a runner process of its own, under a watchdog:

caller ──spawn──> watchdog ──spawn, monitor──> runner
   │                  │ monitors the caller        │
   │                  │ and the registry           │
   └── monitor ───────┼────────────────────────────┤
   ^                  │ kills the runner at the    │
   │                  │ deadline, on cancel, or    │
   │                  │ when the caller or the     │
   │                  │ registry is gone           │
   └───────────── reply, by alias ─────────────────┘
  • The watchdog spawns the runner, so from the first instant some process knows the runner and will stop it. It is the only process that kills a runner (cancel/1 asks it to), so it always knows why a runner died: its own kill, or, when it did not kill, the runner's heap limit.
  • The runner instantiates the prepared component with the policy's WASI imports and limits, calls the function, destroys the instance and replies to the caller's alias. A reply that comes after a cancel or a timeout finds the alias gone and is dropped.
  • The runner traps exits, so a process linked to it, such as an HTTP connection the server closed, cannot kill the call. The watchdog uses kill, which no process can trap.
  • The runner is spawned with max_heap_size, so a call that builds a huge term dies alone. The caller monitors it and is never linked to it: a runner that dies is an error for the caller, never an exit signal.

A call is described by a tool(): the prepared component, the export's name and its definition (parameters and result), and its policy. Arguments are a map keyed by parameter name; a missing option parameter is none. Errors use the classes of docs/design.md, section 3.

An observer in the options receives [wasm_tools, call, start] when the call starts, [wasm_tools, call, stop] when the function returned (a value or an error value), and [wasm_tools, call, exception] when the call was stopped (deadline, cancel, heap limit, caller or registry gone). Measurements hold duration in native time units; metadata holds tool, export, registry, and for stop and exception the class and kind of the outcome (ok for a value). Arguments and results are never in it. A wasi:http request the capabilities refuse is [wasm_tools, http, denied], from the runner, with the call's metadata and the request's scheme, host and port, and its address when an address was refused (wasm_tools_policy:wasi_imports/2). The observer runs in the caller, the runner or the watchdog, so it must be quick; an exception in it is caught and logged once.

Summary

Functions

Wait for a call started with start/3.

Run one call and wait for its result.

Stop a call and discard its outcome.

Check arguments as start/3 does, without starting the call. Only the params and result of the tool are needed.

Give one event to an observer: a call's, a refused HTTP request's, or [wasm_tools, load, stop] from a load. An exception in the observer is caught, and logged the first time that observer fails: a broken observer neither stops the work it watches nor fills the log.

Start one call and return at once.

Types

error()

-type error() :: #{class := atom(), kind := atom(), msg := binary(), ctx := map()}.

job()

-opaque job()

observer()

-type observer() :: fun(([atom()], map(), map()) -> term()).

opts()

-type opts() ::
          #{timeout => timeout(),
            limits => map(),
            context => term(),
            registry => pid(),
            slot => term(),
            observer => observer()}.

tool()

-type tool() ::
          #{prepared := wasm_component:prepared(),
            export := binary(),
            params := [{binary(), term()}],
            result := term(),
            policy := wasm_tools_policy:policy(),
            imports => fun((map()) -> map()),
            name => binary()}.

Functions

await/2

-spec await(job(), timeout()) -> {ok, term()} | {error, error()}.

Wait for a call started with start/3.

Timeout bounds this wait only: when it runs out, the call keeps running and you can wait again; the call's own deadline is the watchdog's.

call(Tool, Args, Opts)

-spec call(tool(), #{binary() => term()} | term(), opts()) -> {ok, term()} | {error, error()}.

Run one call and wait for its result.

Args should be a map keyed by parameter name; anything else is an argument error, not a crash.

cancel/1

-spec cancel(job()) -> ok | {error, error()}.

Stop a call and discard its outcome.

Returns once the runner is gone, so nothing of the call runs afterwards, and leaves no message behind. The call is then finished: do not await/2 it. If it had already returned, its result is dropped.

check(Tool, Args)

-spec check(#{params := [{binary(), term()}], result := term(), atom() => term()},
            #{binary() => term()} | term()) ->
               ok | {error, error()}.

Check arguments as start/3 does, without starting the call. Only the params and result of the tool are needed.

observe/4

-spec observe(observer() | undefined, [atom()], map(), map()) -> ok.

Give one event to an observer: a call's, a refused HTTP request's, or [wasm_tools, load, stop] from a load. An exception in the observer is caught, and logged the first time that observer fails: a broken observer neither stops the work it watches nor fills the log.

start/3

-spec start(tool(), #{binary() => term()} | term(), opts()) -> {ok, job()} | {error, error()}.

Start one call and return at once.

The process that starts a call owns it: only that process can await/2 or cancel/1 it. If it dies, the call is stopped.