leaderelection

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2025 License: GPL-3.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// TTL is the time-to-live for the leader lock
	TTL time.Duration

	// RenewalInterval is how often to renew the leader lock
	RenewalInterval time.Duration

	// NodeID is the unique identifier for this node
	// If empty, a random ID will be generated
	NodeID string
}

Config holds configuration for leader election.

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns a default configuration.

type Elector

type Elector interface {
	// Start begins the leader election process
	Start(ctx context.Context) error

	// Stop gracefully stops the leader election
	Stop(ctx context.Context) error

	// IsLeader returns true if this node is currently the leader
	IsLeader() bool

	// LeadershipChannel returns a channel that receives leadership changes
	// true = gained leadership, false = lost leadership
	LeadershipChannel() <-chan bool

	// GetLeaderID returns the current leader's ID
	GetLeaderID() (string, error)
}

Elector defines the interface for leader election implementations.

type RedisElector

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

RedisElector implements leader election using Redis.

func NewRedisElector

func NewRedisElector(client *redis.Client, log logrus.FieldLogger, keyName string, config *Config) (*RedisElector, error)

NewRedisElector creates a new Redis-based leader elector.

func (*RedisElector) GetLeaderID

func (e *RedisElector) GetLeaderID() (string, error)

GetLeaderID returns the current leader's ID.

func (*RedisElector) IsLeader

func (e *RedisElector) IsLeader() bool

IsLeader returns true if this node is currently the leader.

func (*RedisElector) LeadershipChannel

func (e *RedisElector) LeadershipChannel() <-chan bool

LeadershipChannel returns a channel that receives leadership changes.

func (*RedisElector) Start

func (e *RedisElector) Start(ctx context.Context) error

Start begins the leader election process.

func (*RedisElector) Stop

func (e *RedisElector) Stop(ctx context.Context) error

Stop gracefully stops the leader election.

Jump to

Keyboard shortcuts

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