election

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package election provides a unified manager for cluster and shard leader elections, abstracting the s3lect library and serving both election health endpoints from a single HTTPS server.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ElectionConfig

type ElectionConfig struct {
	// Storage is the s3lect-compatible storage for leader election state
	Storage storage.Storage
	// PeerMode enables peer verification of leadership
	PeerMode bool
	// CACert is the CA certificate for peer verification (nil if peer mode disabled)
	CACert []byte
	// FrequentInterval is how often to check leadership when actively participating
	FrequentInterval time.Duration
	// InfrequentInterval is how often to check leadership when idle
	InfrequentInterval time.Duration
	// LeaderTimeout is how long before a leader is considered dead
	LeaderTimeout time.Duration
	// OnAcquire is called after successfully acquiring leadership
	OnAcquire func(ctx context.Context) error
	// OnLose is called after losing leadership
	OnLose func(ctx context.Context) error
}

ElectionConfig contains configuration for starting an individual election.

type ElectorFactory

type ElectorFactory func(opts s3lect.S3ElectorOptions) (s3lect.Elector, error)

ElectorFactory creates s3lect electors. Used for testing.

type HealthServerConfig

type HealthServerConfig struct {
	// BindAddr is the address to listen on (e.g. ":8443")
	BindAddr string
	// TLSCert is the TLS certificate for the HTTPS server
	TLSCert tls.Certificate
}

HealthServerConfig contains configuration for the election health server.

type Manager

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

Manager manages both cluster and shard leader elections.

func NewManager

func NewManager(cfg ManagerConfig) *Manager

NewManager creates a new election manager.

func (*Manager) EnableClusterPeerMode

func (m *Manager) EnableClusterPeerMode(caCert []byte) error

EnableClusterPeerMode enables peer mode on the cluster elector with the given CA certificate.

func (*Manager) IsClusterLeader

func (m *Manager) IsClusterLeader() bool

IsClusterLeader returns true if this server is the current cluster leader.

func (*Manager) IsShardLeader

func (m *Manager) IsShardLeader() bool

IsShardLeader returns true if this server is the current shard leader.

func (*Manager) StartClusterElection

func (m *Manager) StartClusterElection(ctx context.Context, cfg ElectionConfig) error

StartClusterElection creates and starts the cluster leader election.

func (*Manager) StartHealthServer

func (m *Manager) StartHealthServer(ctx context.Context, cfg HealthServerConfig) error

StartHealthServer creates and starts the election health HTTPS server. It registers handlers for /health/leadership/cluster and /health/leadership/shard. Unstarted electors will return 404 until their election is started.

func (*Manager) StartShardElection

func (m *Manager) StartShardElection(ctx context.Context, cfg ElectionConfig) error

StartShardElection creates and starts the shard leader election.

func (*Manager) Stop

func (m *Manager) Stop(ctx context.Context) error

Stop stops all electors and the health server.

func (*Manager) WaitForClusterElection

func (m *Manager) WaitForClusterElection(ctx context.Context) error

WaitForClusterElection waits for the first cluster election cycle to complete.

type ManagerConfig

type ManagerConfig struct {
	// ClusterID is the cluster identifier
	ClusterID string
	// ShardID is the shard identifier (e.g. zone shard ID)
	ShardID string
	// ServerID is this server's unique identifier
	ServerID string
	// ServerAddr is the address other servers use for peer verification
	ServerAddr string
	// Logger for election events
	Logger *slog.Logger
}

ManagerConfig contains configuration for the election manager.

Jump to

Keyboard shortcuts

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