Actors

An actor is a class: a data type is its state, and its instance of the Actor trait is its methods. Code that works with actors is written once against the trait (actorOf, tell, ask, become), so every class gets it.

The Actor trait

trait Actor a where
  type Message a
  settings :: Settings a                    -- what the attributes above an instance set
  settings = defaultSettings
  receive  :: Context a -> a -> Message a -> Step a

trait Initial a where
  initial :: Parameters -> a                -- how an actor of the class is made from parameters alone

A class names its messages with type Message, and answers each one in receive.

A class

data Account    = Account { owner :: String, balance :: Int }
data AccountMsg = Deposit Int (Reply Int) | Withdraw Int (Reply Int) | Balance (Reply Int)

@route("account/{id}")
@mailbox(100)
instance Actor Account where
  type Message Account = AccountMsg
  receive ctx acc msg = case msg of
    Deposit n r  -> respond r (acc.balance + n) >> continue acc { balance = acc.balance + n }
    Withdraw n r -> case lt acc.balance n of
      True  -> refuse r (Fault "InsufficientFunds" acc.owner) >> continue acc
      False -> respond r (acc.balance - n) >> continue acc { balance = acc.balance - n }
    Balance r    -> respond r acc.balance >> continue acc

instance Initial Account where
  initial params = Account (param "id" params) 0

accountOf :: ActorSystem -> String -> Address Account
accountOf system id = actorOf system (parameters [("id", id)])
ask alice (\r -> Deposit 100 r)      -- Right 100
ask alice (\r -> Withdraw 30 r)      -- Right 70
ask alice (\r -> Withdraw 500 r)     -- Left (Fault "InsufficientFunds" "alice")
routeOf (refOf alice)                -- "/user/account/alice"

Receive and pattern matching

receive ctx state msg is called once per message, one message at a time: an actor never handles two messages at once, so its state needs no locks. It matches the message by its constructors and returns a step.

A message that expects an answer carries a Reply. Answer it with respond r value, or refuse it with refuse r (Fault kind detail): the asker gets Left with that fault. A Fault is a kind (what callers and supervisors match on) and a detail.

Steps

Step What happens next
continue s The next message is handled with state s
become methods s The next messages go to methods instead of receive
becomeUnstashing methods s The same, and every stashed message is handled again first
stash s This message waits until an unstash
unstash s, unstashAll s The oldest stashed message (or all of them) comes back first
stop s The actor stops after this message
failWith kind detail s The message fails: its asker gets the fault, and the actor’s parent decides what to do

Effects come before the step with >>: respond r x >> continue s. Inside an engine-level behaviour (see Lifecycle) write after effect step.

Attributes

The attributes above an instance decorate its settings:

Attribute Effect
@route("account/{id}") Where the actor lives; the template’s parameters come from those it is made with
@mailbox(16) At most 16 waiting messages; past that the oldest is dropped
@backpressure(16) At most 16 waiting messages; past that the sender waits for room
@priority(weight) weight :: Message a -> Int; the heaviest waiting message is handled first
@supervision(strategy) How the actor’s failing children are handled (see Supervision)
@passivateAfter(30000) The actor stops after 30 s without a message; its route makes it again on demand
@ephemeral Every message is handled in a fiber of its own, from the first state (see Ephemeral Actors)

An instance without attributes has a mailbox in order, no route of its own ($<pid> under its parent), and restarts its failing children one for one.

Making actors

Function Makes
actorOf system params An actor of a class under /user, made with initial params
actorWith system params state An actor whose first state is given; the class needs no Initial
childActorOf ctx params A child of the actor whose context this is; it stops with its parent, which supervises it

The class is chosen by the address’s type, so a helper with a signature says which:

accountOf :: ActorSystem -> String -> Address Account
accountOf system id = actorOf system (parameters [("id", id)])

Actors made with their state

Some actors depend on other actors, a store or a key: values that parameters, which are text, cannot carry. Give the state with actorWith; such a class has no Initial, so nothing can make one without its dependencies:

data Teller    = Teller { till :: Address Account, served :: Int }
data TellerMsg = Pay Int (Reply Int) | Served (Reply Int)

instance Actor Teller where
  type Message Teller = TellerMsg
  receive ctx t msg = case msg of
    Pay n r  -> respond r (balanceAfter (ask t.till (\a -> Deposit n a))) >> continue t { served = t.served + 1 }
    Served r -> respond r t.served >> continue t

tellerFor :: ActorSystem -> Address Account -> Address Teller
tellerFor system account = actorWith system (parameters []) (Teller { till = account, served = 0 })

Asking from inside an actor

An actor may ask another, as the teller asks its account: it waits for the answer, and handles no other message meanwhile. Do not ask an actor that is waiting on you, and do not ask your own child something that may fail: a failing child waits for its parent’s decision, and its parent is busy waiting for the child. Hand the child’s address to whoever needs it, or tell it and let it answer with a message.

Where next