wasm_tools_call (wasm_tools v0.1.0)
View SourceOne 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/1asks 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
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
-opaque job()
-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
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.
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.
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.
-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.
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.
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.