Consensus

Functions

Public API reference for the Consensus extension

Extension Entry Points

FunctionDescription
NewExtension(opts ...ConfigOption) forge.ExtensionCreate the consensus extension with functional options
NewExtensionWithConfig(config Config) forge.ExtensionCreate with a complete config
DefaultConfig() ConfigReturns the default configuration

Extension Accessors

MethodDescription
Service() ConsensusServiceAccess the consensus service
IsLeader() boolCheck if this node is the current leader
GetLeader() (string, error)Get the current leader's node ID
GetRole() stringGet the current role (leader, follower, candidate)
GetStats() map[string]anyGet consensus statistics
GetClusterInfo() map[string]anyGet cluster membership info

ConsensusService Interface

Operations

MethodDescription
Apply(ctx, cmd) (any, error)Apply a command through the Raft log (write operation)
Read(ctx, query) (any, error)Read from the state machine (consistent read)

Lifecycle

MethodDescription
Start(ctx) errorStart the Raft node
Stop(ctx) errorStop the Raft node
HealthCheck(ctx) errorCheck node and cluster health

Leadership

MethodDescription
IsLeader() boolCheck if this node is the leader
GetLeader() (string, error)Get the leader node ID
GetRole() stringGet the current role

Cluster

MethodDescription
GetStats() map[string]anyRuntime statistics
GetClusterInfo() map[string]anyCluster membership information
UpdateConfig(config) errorUpdate configuration at runtime

LeadershipChecker

MethodDescription
NewLeadershipChecker(service ConsensusService)Create a leadership checker
RequireLeader() boolReturns true if current node is leader
RequireQuorum() boolReturns true if cluster has quorum

Error Helpers

FunctionDescription
NewNotLeaderError(leaderID) errorCreate a not-leader error with leader hint
NewNoLeaderError() errorCreate a no-leader error
NewTimeoutError(operation) errorCreate a timeout error
NewNoQuorumError(available, required) errorCreate a quorum error
IsNotLeaderError(err) boolCheck if error is not-leader
IsNoLeaderError(err) boolCheck if no leader elected
IsNoQuorumError(err) boolCheck if quorum lost
IsRetryable(err) boolCheck if error is transient
IsFatal(err) boolCheck if error is unrecoverable

How is this guide?

On this page