Addresses & Routes
An actor is reached through its address. Inside one process the address is typed by the actor’s class; across a cluster the actor is reached by its route, and the host decides where the route runs.
Addresses
data Address a -- an actor of class a, told only a's messages
tell :: Address a -> Message a -> ()
ask :: Address a -> (Reply r -> Message a) -> Either Fault r
askWithin :: Int -> Address a -> (Reply r -> Message a) -> Either Fault r
- tell delivers the message, or, when the actor has stopped, records it as a dead letter.
- ask makes the message with a reply, sends it, and waits. A stopped actor answers at once with
Left (Fault "ActorStopped" route); nothing is left waiting. - askWithin ms gives up after
msmilliseconds withLeft (Fault "Timeout" ...). See Timeouts.
self ctx is an actor’s own address, to tell itself or to hand to others. refOf address is the engine’s reference
underneath (ActorRef msg), which timers, death watch and topics take.
Routes
A route names where an actor lives under its guardian: /user/counter, /user/account/alice. @route gives a
class its route; without one an actor is $<pid> under its parent.
Parameterized routes
A route template has parameters in braces. One template names a family of actors, one per value:
@route("account/{id}")
instance Actor Account where ...
instance Initial Account where
initial params = Account (param "id" params) 0
accountOf :: ActorSystem -> String -> Address Account
accountOf system id = actorOf system (parameters [("id", id)])
routeOf (refOf (accountOf system "alice")) -- "/user/account/alice"
parameters [(name, value)] makes the parameters (names are lowercased); param name params reads one ("" when it
is missing). The same parameters fill the route and reach initial.
Route resolution
Actor.Route has the pieces underneath: routeTemplate "account/{id}", parameterNames, and resolveRoute, which
fills a template from parameters and fails (Left) when one is missing.
How addresses work across topologies
In one process an Address is a channel: telling is putting a message in the actor’s mailbox.
Across a cluster, a client does not hold the actor’s channel. It asks a route through its host’s client:
askRoute :: ActorClient -> String -> String -> Bytes -> Int -> Either Fault Bytes
tellRoute :: ActorClient -> String -> String -> Bytes -> ()
askRoute client "account/alice" "String" payload 5000
The client is the same type in every topology. Standalone, the host makes the route’s actor here the first time it is asked. In a cluster, the request goes to a cluster node, which finds the satellite that hosts the route (or has one make it) and forwards the answer back. The calling code does not change. See Topology & Hosting.
Messages that cross the network are bytes: a hosted class receives HostedMsg typeName payload reply and answers
with answer reply (Right bytes). Serialization turns values into those bytes.
Example: one actor references another
An actor that holds another’s address gets it as part of its state:
data Teller = Teller { till :: Address Account, served :: Int }
tellerFor :: ActorSystem -> Address Account -> Address Teller
tellerFor system account = actorWith system (parameters []) (Teller { till = account, served = 0 })
Or it makes the other as its child, which it then supervises:
Hire r -> respond r (childActorOf ctx (parameters [])) >> continue f
Where next
- Topology & Hosting: routes across a cluster.
- API: Address