src/counter/counter.oxl
/// A counter per key: the state lives in the shard group of the key and survives the loss of a node.
pub actor Counter sharded by id: u64 {
var total: i64 = 0
on activate { print("counter {id} activated on {cluster.self().name}") }
msg add(x: i64) -> i64 {
total += x
total
}
msg get() -> i64 { total }
}
pub(api) service Gate {
@public rpc add(id: u64, x: i64) -> i64
@public @readonly rpc get(id: u64) -> i64
}
impl Gate {
rpc add(id, x) {
match Counter(id).add(x) {
i64 v => v
NetError _ => -1
}
}
rpc get(id) {
match Counter(id).get() {
i64 v => v
NetError _ => -1
}
}
}
// `oxl dev` assigns the roles in order: edge first, the others store (OXL_ROLE overrides it).
cluster counters {
role edge { serves Gate; runs leader("traffic"); replicas 1.. }
role store { journal; shards 8; replication 3; hosts Counter }
assign by env("OXL_ROLE") else auto
}Исходник: examples/counter-cluster/src/counter/counter.oxl