Cluster
The Cluster module provides cluster-wide operations for distributed systems. It operates across all nodes in a Kit cluster, providing a higher-level abstraction over individual node operations.
Cluster vs Node
While Node manages individual connections, Cluster provides operations that span all connected nodes automatically.
Cluster Information
Cluster.nodes
() -> List String
Returns a list of all nodes in the cluster (self + connected nodes).
nodes = Cluster.nodes
println "Cluster nodes: ${nodes}"
Cluster.size
() -> Int
Returns the number of nodes in the cluster (always at least 1).
if Cluster.size < 3 then
println "Warning: Cluster below minimum size"
Broadcasting
Cluster.broadcast
String -> a -> Result Int ClusterError
Sends a message to all actors with the given name across all nodes. Returns
Ok count of successful sends.# Broadcast reset to all "worker" actors in the cluster
match Cluster.broadcast "worker" :reset
| Ok count -> println "Reset ${count} workers"
| Err e -> println "Broadcast failed: ${e}"
Error Types
type ClusterError =
| BroadcastFailed { message: String }
| ClusterError { message: String }
Complete Example
# Start this node
Node.start "master@192.168.1.1:9000"
# Connect to worker nodes
Node.connect "worker-1@192.168.1.2:9000"
Node.connect "worker-2@192.168.1.3:9000"
# Check cluster status
println "Cluster has ${Cluster.size} nodes"
println "Nodes: ${Cluster.nodes}"
# Broadcast a command to all workers
Cluster.broadcast "worker" {task: "process-data"}
# Monitor cluster health
if Cluster.size < 3 then
println "Warning: Some workers may be down"