Help us shape the future of Go! Take the 2026 Go Developer Survey

election

package
v0.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 11, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package election provides Raft-based leader election for multi-core HA setups

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cluster

type Cluster struct {
	// contains filtered or unexported fields
}

Cluster represents a Raft consensus cluster for leader election

func NewCluster

func NewCluster(config Config) (*Cluster, error)

NewCluster creates a new Raft cluster for leader election

func (*Cluster) Apply

func (c *Cluster) Apply(cmd []byte, timeout time.Duration) error

Apply applies a command to the Raft cluster

func (*Cluster) GetClusterSize

func (c *Cluster) GetClusterSize() int

GetClusterSize returns the number of nodes in the cluster

func (*Cluster) GetLeader

func (c *Cluster) GetLeader() string

GetLeader returns the address of the current leader

func (*Cluster) GetRaft

func (c *Cluster) GetRaft() *raft.Raft

GetRaft returns the underlying Raft instance

func (*Cluster) GetState

func (c *Cluster) GetState() State

GetState returns the current state of this node

func (*Cluster) IsLeader

func (c *Cluster) IsLeader() bool

IsLeader returns true if this node is the leader

func (*Cluster) Leave

func (c *Cluster) Leave() error

Leave gracefully removes this node from the cluster

func (*Cluster) ListPeers

func (c *Cluster) ListPeers() []string

ListPeers returns the list of peers in the cluster

func (*Cluster) Shutdown

func (c *Cluster) Shutdown() error

Shutdown gracefully shuts down the Raft cluster

func (*Cluster) Subscribe

func (c *Cluster) Subscribe() <-chan StateChange

Subscribe subscribes to state changes

func (*Cluster) Unsubscribe

func (c *Cluster) Unsubscribe(ch <-chan StateChange)

Unsubscribe unsubscribes from state changes

type Config

type Config struct {
	// NodeID is the unique identifier for this node
	NodeID string
	// Addr is the address this node listens on
	Addr string
	// Peers is the list of all peer addresses in the cluster
	Peers []string
	// BootstrapExpect is the number of nodes needed to bootstrap
	BootstrapExpect int
	// HeartbeatTimeout is the timeout for leader heartbeats
	HeartbeatTimeout time.Duration
	// ElectionTimeout is the timeout for elections
	ElectionTimeout time.Duration
	// CommitTimeout is the timeout for commit operations
	CommitTimeout time.Duration
}

Config holds configuration for the Raft cluster

type State

type State string

State represents the current state of a node in the cluster

const (
	StateFollower  State = "follower"
	StateCandidate State = "candidate"
	StateLeader    State = "leader"
)

type StateChange

type StateChange struct {
	OldState State
	NewState State
	Leader   string
}

StateChange represents a change in cluster state

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL