wasm_tools_registry (wasm_tools v0.1.0)

View Source

A registry: named tools from several components, in one process you supervise.

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

You need one when an application manages several components, or wants a bound on concurrent calls, or counters, or to expose tools through MCP. For a single component, call it directly (wasm_tools:call/3).

{ok, Reg} = wasm_tools:start_link(#{max_concurrency => 64}),
{ok, Calc} = wasm_tools:load({file, "calculator.wasm"}),
{ok, [<<"calc_add">>, <<"calc_multiply">>]} =
    wasm_tools:add(Reg, Calc, #{namespace => <<"calc">>}),
{ok, 42} = wasm_tools:call(Reg, <<"calc_add">>, #{<<"a">> => 20, <<"b">> => 22}).
  • Registries are independent: each has its own names, components, counters and bound. There is no global registry.
  • add/3 keeps a holder of its own for the component, so the registry does not depend on the process that loaded it. namespace prefixes each name with Namespace_; exports picks which exports become tools (export names or tool names; every callable export by default). Names are computed among the exports picked, so two ops interfaces can be added one at a time. A name that collides, inside the selection or with a tool already there, refuses the whole add: nothing is renamed or replaced.
  • A call checks the tool out of the registry (one message), which refuses it with #{class := overloaded} past max_concurrency, then runs in processes of its own, as any call. Its watchdog checks it back in when it ends.
  • remove/2 returns once the names are gone: no call starts on them after it. Calls already running finish, unless remove/3 is given kill => true.
  • When the registry stops, every call it started is stopped, and the holders it kept are released.
  • stats/1 reads counters kept with OTP counters: active, calls, ok, errors by class, rejected.

Summary

Functions

Add a component's exports as tools. Returns the names given.

A child spec for your supervisor.

Remove tools. With kill => true, calls running on them are stopped.

Start a registry linked to the caller. name registers it locally.

Types

opts()

-type opts() ::
          #{name => atom(),
            max_concurrency => pos_integer() | infinity,
            observer => wasm_tools_call:observer()}.

registry()

-type registry() :: pid() | atom().

Functions

add(Registry, Component, Opts)

-spec add(registry(), wasm_tools_component:component(), map()) -> {ok, [binary()]} | {error, map()}.

Add a component's exports as tools. Returns the names given.

child_spec(Opts)

-spec child_spec(opts()) -> supervisor:child_spec().

A child spec for your supervisor.

describe(Registry, Name)

-spec describe(registry(), binary()) -> {ok, map()} | {error, map()}.

handle_call/3

handle_cast(Msg, State)

handle_info/2

init(Opts)

list(Registry)

-spec list(registry()) -> {ok, [map()]}.

remove(Registry, Names)

-spec remove(registry(), [binary()]) -> ok.

remove(Registry, Names, Opts)

-spec remove(registry(), [binary()], map()) -> ok.

Remove tools. With kill => true, calls running on them are stopped.

start_link(Opts)

-spec start_link(opts()) -> {ok, pid()} | {error, term()}.

Start a registry linked to the caller. name registers it locally.

stats(Registry)

-spec stats(registry()) -> map().

stop(Registry)

-spec stop(registry()) -> ok.

terminate/2