The platform · actors
State, one message at a time
A shopping cart, a counter, a game room: state that many requests change at once, and that must stay right. An actor holds it, handles one message at a time, in order, and keeps it in the database across restarts. For what only matters now (who is online, what was just said), use live state in memory.
Kinds and methods#
An actor is a kind and a key: the cart of member 7, the counter called visits. A kind has a name and a starting state (initial, computed from the key). Its methods are what its actors can do, each with its own input and reply types. A method's turn gets the actor (me), the app's context (what init! made), the current state and the input, and returns the next state and the reply.
## A count, one per key, that outlives requests and restarts: a kind (a type with `name` and
## `initial`) and its methods.
Counter := [].{
name = |{}| "counter"
initial = |_key| { count: 0 }
## The type of its methods: the kind, the app's context (`--context-file`'s text), the key, the
## count; then each one's input and reply.
Method(input, reply) : Actor.Method(Counter, Str, Str, { count : I64 }, input, reply)
…
## Adds `input` to the count: the count after.
add : Counter.Method(I64, I64)
add = Actor.method("add", |{ state, input, .. }| { state: { count: state.count + input }, reply: state.count + input })
## The count.
get : Counter.Method({}, I64)
get = Actor.method("get", |{ state, .. }| { state, reply: state.count })
}- Nobody creates or ends an actor. A call to any key finds that actor as its last turn left it, or new, starting from
initial(key). - One turn at a time, in order. Each actor has a mailbox in the server and handles its messages one turn after another, whatever method each is for. A hundred asks at once each see the state the one before left: no lost updates, no locks to write.
- A bad call is an error, not a crash. Methods are typed, so this is rare: a caller built against an older or newer kind may send an input a method doesn't take, or name a missing method. That, a crash inside a method, and an unknown kind are each logged as an error and reported to the caller. The actor's state stays as it was, and it goes on to its next turn.
- Kept in the database (
rocx_actors): read at an actor's first turn, written after each turn that changes it. An actor idle for 15 minutes (--actor-idle-ms) leaves memory; the next call wakes it from the database.
How the types fit together. A kind is a type of your app's with name and initial (checked through Actor.Kind). It names its methods' type once, Method(input, reply) : Actor.Method(Counter, Str, Str, { count : I64 }, input, reply): the kind, the app's context, the key and the state. Each method is then a value, Actor.method(name, run!), annotated with that type (Counter.Method(I64, I64)), which makes it that kind's.
Keys. A key is any type with to_str, which names its actor (in the database too), and from_quote, which reads that name back and lets a string literal be a key (Actor.Keyed). Str has both; so can your own type, like [YouTube, Instagram(Str)]. A method gets the key as its own type (me.key()), so it can match on it rather than parse it, and another kind's key type doesn't compile.
rocx build finds the methods your modules define (Actor.method, at the top of a module's block) and lists them in the program, so the server can run an actor when nobody waits on it: for a tell or a timer. It also checks that each kind's name (name = |{}| "…") is unique. Two methods of one kind with the same name stop the server at start.
Asking and telling#
("POST", path) if path.starts_with("/actors/count/") => {
by : I64
by = I64.from_str(param(request.query, "by")) ?? 1
answered_actor(Counter.add.ask!(after(path, "/actors/count/"), by))Counter.add.ask!(key, input) waits for the reply, typed as the method says, or ActorErr(why) if none comes. Counter.add.tell!(key, input) doesn't wait: its turn comes after those before it, and its reply goes nowhere (a problem is logged).
("POST", "/actors/tell") => {
Counter.add.tell!(run, I64.from_str(param(request.query, "by")) ?? 1)
text(200, "told")
}An actor may ask another. An ask that would wait forever is refused at once: an actor asking itself, or any actor whose turn waits on its answer. An ask unanswered after 30 seconds is given up, and its message dropped if its turn hasn't come.
Timers#
A method gets the actor itself, me: its key (me.key()) and its timers. me.after!(ms, method, input) calls one of its kind's methods later (another kind's doesn't compile) and returns the timer, which Actor.cancel! stops. To repeat, let each call set the next. A method that names itself is a function of {}, because a value can't be defined in terms of itself:
## Counts its own ticks: `start` sets a timer for itself, and each tick sets the next
## (`me.after!`) until `stop` cancels the one waiting (`Actor.cancel!`).
Ticker := [].{
name = |{}| "ticker"
initial = |_key| { ticks: 0, timer: 0, on: Bool.False }
Method(input, reply) : Actor.Method(Ticker, Str, Str, { ticks : I64, timer : U64, on : Bool }, input, reply)
start : Ticker.Method(U64, I64)
start = Actor.method("start", |{ me, state, input, .. }| { state: { ticks: state.ticks, timer: me.after!(input, Ticker.tick({}), input), on: Bool.True }, reply: state.ticks })
## A tick sets the next: a method that names itself, so a function of `{}`.
tick : {} -> Ticker.Method(U64, I64)
tick = |{}|
Actor.method(
"tick",
|{ me, state, input, .. }|
if (state.on) {
{ state: { ticks: state.ticks + 1, timer: me.after!(input, Ticker.tick({}), input), on: Bool.True }, reply: state.ticks + 1 }
} else {
{ state, reply: state.ticks }
},
)
stop : Ticker.Method({}, I64)
stop = Actor.method(
"stop",
|{ state, .. }| {
Actor.cancel!(state.timer)
{ state: { ticks: state.ticks, timer: 0, on: Bool.False }, reply: state.ticks }
},
)
ticks : Ticker.Method({}, I64)
ticks = Actor.method("ticks", |{ state, .. }| { state, reply: state.ticks })
}Counter.add.tell_after!(key, ms, input) does the same from anywhere: a request, a job, another actor.
With a database, a timer survives a restart. It is a row in rocx_actor_timers, written at once (outside the caller's open transaction, as a tell is sent at once) and deleted once its turn has ended; the server sets every row again at start. So a timer's call is made at least once: if the server is killed mid-turn, it runs again after the restart. Actor.cancel! stops a timer that is waiting, or one that fired but whose turn hasn't come. Without a database, timers are in memory, and a restart loses them.
Not yet#
- An actor's state is JSON in one row, written after each message that changes it: fine for a cart, not for a ledger.
- Tells are in memory. A restart loses unhandled ones (timers survive); use a job for work that must survive one.
Every kind of state, side by side: which to use.