leaderelection

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2026 License: GPL-3.0 Imports: 12 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 (for logging/metrics only)
	// 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

	// OnLeadershipChange registers a callback for guaranteed leadership notification.
	// The callback is invoked synchronously when leadership status changes.
	// Multiple callbacks can be registered and will be invoked in registration order.
	//
	// Important: Keep callbacks fast (< 100ms) to avoid delaying leadership renewal.
	// For long-running operations, spawn a goroutine within the callback.
	OnLeadershipChange(callback LeadershipCallback)
}

Elector defines the interface for leader election implementations.

type LeadershipCallback added in v0.1.5

type LeadershipCallback func(ctx context.Context, isLeader bool)

LeadershipCallback is a function invoked when leadership status changes. The callback is invoked synchronously - implementations should return quickly (< 100ms) to avoid delaying leadership renewal. Long-running operations should be spawned in a separate goroutine.

type RedisElector

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

RedisElector implements leader election using Redis with redsync.

func NewRedisElector

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

NewRedisElector creates a new Redis-based leader elector using redsync.

func (*RedisElector) IsLeader

func (e *RedisElector) IsLeader() bool

IsLeader returns true if this node is currently the leader.

func (*RedisElector) OnLeadershipChange added in v0.1.5

func (e *RedisElector) OnLeadershipChange(callback LeadershipCallback)

OnLeadershipChange registers a callback for guaranteed leadership notification.

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