raft

The raft module (built on hashicorp/raft with BoltDB storage) runs a Raft consensus node. You provide an applyLog callback; the replicated-state-machine Apply step dispatches each committed entry to it. Nodes are referenced by an integer handle. Import it with import raft.

Functions

FunctionSignatureDescription
startraft.start(opts) → intStart a node; returns a handle
applyraft.apply(h: int, command: string) → boolReplicate one log entry through the cluster
isLeaderraft.isLeader(h: int) → boolWhether this node is the leader
leaderraft.leader(h: int) → stringAddress of the current leader
stateraft.state(h: int) → stringNode state (e.g. "Leader", "Follower", "Candidate")
addPeerraft.addPeer(h: int, id: string, addr: string) → boolAdd a voter to the cluster
shutdownraft.shutdown(h: int) → boolStop the node

opts for start:

KeyDescription
nodeIdUnique node identifier
addrBind address for Raft transport
storePathDirectory for the BoltDB log store
bootstraptrue to bootstrap a new single-node cluster
peersInitial peer set
applyLogCallback fn(command) run for each committed entry

Example

import raft

let node = raft.start({
    nodeId: "n1",
    addr: "127.0.0.1:7000",
    storePath: "./raft-data",
    bootstrap: true,
    applyLog: fn(command) {
        println("applied: " + command)
    },
})

if raft.isLeader(node) {
    raft.apply(node, "set x = 1")
}
Standard library · View as Markdown · llms-full.txt