Roc Solid
Markup in Roc · pages on a native server · components in WebAssembly

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.

server/test/app/main.roc, in part
## 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 })
}

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#

server/test/app/main.roc, in part
("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).

server/test/app/main.roc, in part
("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:

server/test/app/main.roc, in part
## 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#

Every kind of state, side by side: which to use.