wasm_actor (wasm v0.16.0)

View Source

A WebAssembly component that handles typed messages, one at a time, as an OTP process. docs/actors.md is the guide; this is the reference.

An actor owns one component instance for its whole life, so the guest keeps its state from one message to the next. It takes messages from Erlang through send/2 and from its own guest, which posts them to a destination you wire when you start it. It is a gen_server, so you put it under your own supervisor like any other child.

The guest implements this world. The message type is yours: name it what you like, give it the cases your application needs, and pass the same type to start_link/2 as message. The send-error enum is the runtime's and must keep its two cases in this order.

package demo:actors@0.1.0;

interface protocol {
    variant message { add(u32), added(u32) }
    enum send-error { full, closed }
}

world counter-actor {
    use protocol.{message, send-error};
    import post: func(msg: message) -> result<_, send-error>;
    export handle: func(msg: message);
}

What a successful send means

send/2 answers ok when the message is in the destination's queue. It does not wait for the actor to run, so a handler can post to any actor, itself included, and return. Admission promises two things:

  • Order. Two messages one sender admits to one actor are handled in the order they were sent. Messages from different senders interleave.
  • A bound. An actor's queue never holds more than its capacity, however many processes send at once. The message being handled does not count.

It does not promise that the message is handled. An actor that stops, by a trap, a supervisor or a kill, drops what is still in its queue. If you need to know a message was handled, have the guest post a reply.

When a send is refused

  • {error, full}: the queue is at its capacity. Nothing was queued. The actor is alive and working, so you may retry later, drop the message, or slow down the sender. Retrying in a tight loop from inside a handler can never succeed when the destination is the actor itself.
  • {error, closed}: no actor is there: it stopped, it was never started, or the name is not registered. A supervised actor that is restarting has a new pid, so send to its registered name, or look the new pid up, and retry.

A guest sees the same two answers, as the send-error result of post.

A message that does not fit the destination's type is not refused: send/2 raises badarg, because the mistake is the caller's. A guest post can only make that mistake if you wired two actors of different message types together, and then the posting actor stops with the reason.

When the guest fails

A trap in handle, or any error from the call, stops the actor with {handle_failed, Error} as its exit reason, Error being the error map the runtime gives for the call. The instance is destroyed before the process exits, so its memory goes back to the node. A supervisor then starts a new actor with a new instance, whose state starts from the beginning.

What it needs

The wasm application must be running: senders find an actor through a table it owns.

Summary

Functions

Queue Msg for the actor at Dest, a pid or a registered name.

Start an actor over a component, linked to the caller.

Types

dest()

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

opts()

-type opts() ::
          #{message := wasm_canon:desc(),
            to => to(),
            capacity => pos_integer(),
            register => atom(),
            imports => #{{binary(), binary()} => function()},
            limits => map()}.

to()

-type to() :: dest() | self | fun((term()) -> ok | {error, full | closed}).

Functions

handle_call(Req, From, State)

handle_cast(Msg, State)

handle_info/2

init/1

send(Dest, Msg)

-spec send(dest(), term()) -> ok | {error, full | closed}.

Queue Msg for the actor at Dest, a pid or a registered name.

Answers ok once the message is in the queue, {error, full} when the queue is at its capacity and {error, closed} when no actor is there. It never waits for the actor. Raises badarg when Msg does not fit the actor's message type.

start_link(Component, Opts)

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

Start an actor over a component, linked to the caller.

Component is the component's bytes, or what wasm_component:prepare/1 made of them: prepare once and every restart skips decoding. Keep it out of a child spec, which a supervisor prints in every crash report, and hold it (wasm_component:hold/1) from a process that outlives the actor, so a restart finds its modules loaded.

Opts:

  • message (required): the message type, as a wasm_canon descriptor. It must be a plain value: no resources, futures or streams, which belong to one instance and cannot travel to another.
  • to: where the guest's post goes. An actor's pid or registered name, read again on every post so a restarted actor is found by its name; self; or a fun(Msg) that answers ok or {error, full | closed}, which is how an Erlang process that is not an actor receives. Left out, every post answers closed.
  • capacity: how many messages the queue holds, 1024 when left out.
  • register: a local name to register the actor under. It is registered once the queue exists, so a sender that finds the name can send to it; a name already taken is {error, {already_started, Pid}}.
  • imports: more host functions for the guest, keyed as for wasm_component:instantiate/2. post is the actor's own.
  • limits: instance limits, as wasm_component:instantiate/3 takes them.

The instance is made before this returns, so a component that does not instantiate, or has no handle export, is an {error, Reason} here and not a crash later.

terminate/2