Actor
The traits (Actor.Class)
trait Actor a where
type Message a
settings :: Settings a
settings = defaultSettings
receive :: Context a -> a -> Message a -> Step a
trait Initial a where
initial :: Parameters -> a
data Address a -- an actor of class a
data Context a -- what a method is given besides its state and message
type Step a = Turn (Message a) a -- what a method answers
Settings attributes
| Attribute |
Function |
Effect |
@route(template) |
route :: String -> Settings a -> Settings a |
The actor’s route, filled from its parameters |
@mailbox(n) |
mailbox :: Int -> … |
Bounded, the oldest dropped |
@backpressure(n) |
backpressure :: Int -> … |
Bounded, the sender waits |
@priority(weight) |
priority :: (Message a -> Int) -> … |
Heaviest first |
@supervision(strategy) |
supervision :: Strategy -> … |
How its children are supervised |
@passivateAfter(ms) |
passivateAfter :: Int -> … |
Stops when idle |
@ephemeral |
ephemeral :: Settings a -> Settings a |
A fiber per message, from the first state |
Making and using actors
| Function |
Type |
actorOf |
(Actor a, Initial a) => ActorSystem -> Parameters -> Address a |
actorWith |
Actor a => ActorSystem -> Parameters -> a -> Address a |
childActorOf |
(Actor b, Initial b) => Context a -> Parameters -> Address b |
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 |
self |
Context a -> Address a |
become |
(Context a -> a -> Message a -> Step a) -> a -> Step a |
becomeUnstashing |
(Context a -> a -> Message a -> Step a) -> a -> Step a |
terminate |
Address a -> () |
parameters |
[(String, String)] -> Parameters |
param |
String -> Parameters -> String |
refOf |
Address a -> ActorRef (Message a) |
ctxOf |
Context a -> Ctx (Message a) |
behaviorOf |
(Actor a, Initial a) => Parameters -> Behavior (Message a) a |
Steps (Actor.Behavior)
| Function |
Type |
Meaning |
continue |
state -> Turn msg state |
Next message, this state |
becomeWith |
(Ctx msg -> state -> msg -> Turn msg state) -> state -> Turn msg state |
Other methods (engine level) |
unbecome |
state -> Turn msg state |
Back to the methods before |
stash |
state -> Turn msg state |
Keep this message for later |
unstash, unstashAll |
state -> Turn msg state |
Bring stashed messages back |
stop |
state -> Turn msg state |
Stop after this message |
failWith |
String -> String -> state -> Turn msg state |
Fail with a fault of that kind and detail |
after |
() -> Turn msg state -> Turn msg state |
An effect, then a step |
Behaviours (Actor.Behavior)
The engine underneath classes. A behaviour is built from a first state and a receive function, then decorated:
| Function |
Type |
actor |
(() -> state) -> (Ctx msg -> state -> msg -> Turn msg state) -> Behavior msg state |
behavior |
String -> (() -> state) -> (Ctx msg -> state -> msg -> Turn msg state) -> Behavior msg state |
withRoute |
String -> Behavior msg state -> Behavior msg state |
onPreStart |
(Ctx msg -> state -> state) -> Behavior msg state -> Behavior msg state |
onPostStop |
(Ctx msg -> state -> ()) -> … |
onPreRestart |
(Ctx msg -> state -> Fault -> ()) -> … |
onPostRestart |
(Ctx msg -> state -> Fault -> state) -> … |
withSupervision |
Strategy -> … |
withCapacity, withBackpressure |
Int -> … |
withPriority |
(msg -> Int) -> … |
inMailbox |
Mailbox msg -> … |
withPassivation |
Int -> … |
asEphemeral |
Behavior msg state -> Behavior msg state |
Each can be written as an attribute on the binding: @B.withRoute("console"), @B.onPreStart(opened).
Types (Actor.Types)
data Fault = Fault String String -- kind, detail
data Reply r -- where an ask is answered
data Directive = Restart | Resume | Stop | Escalate | Propagate
data Strategy = OneForOne Decider | AllForOne Decider
data Mailbox msg = Fifo | Bounded Int | Prioritized (msg -> Int) | Backpressured Int
data Turn msg state = Continue state | Become (Receive msg state) state | Unbecome state | Stash state
| Unstash state | UnstashAll state | BecomeUnstashAll (Receive msg state) state
| Stopped state | Failed Fault state