Actors
An actor owns its state (var), available only from its msg handlers. Messages are processed one at a time, in the order
they arrive from each sender, so no locks are needed (§8.8). spawn Session() creates a local actor and returns an
ActorRef; a msg call suspends until the reply and the chain -> passes the result on.
actor Session {
var total: i32 = 0
msg add(x: i32) {
print("add {x}")
total += x
}
msg sum() -> i32 {
total
}
}
fn show(n: i32) io {
print("total {n}")
}
fn main() io {
let s = spawn Session()
s.add(5) -> s.add(7) -> s.sum() -> show(it)
}Output
add 5 add 7 total 12
Sharded actors#
actor Account sharded by id: u64 is one instance per key, distributed over the nodes of the roles that declared
hosts Account (shards, rebalancing and persistence: §12.4, §12.5). A call Account(1).deposit(5) looks the same, but
its result includes NetError: the actor may live on another node. On a single node hosts activates the actors locally:
actor Account sharded by id: u64 {
var balance: i64 = 0
msg deposit(x: i64) { balance += x }
msg get() -> i64 { balance }
}
cluster demo {
role all { hosts Account }
}
fn main() io, net {
Account(1).deposit(5)!
match Account(1).get() {
i64 n => print("{n}")
NetError e => print("net error")
}
}Output
5