elector

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package elector elects one leader per cluster with a lease row whose times come from the client clock. The leader runs the maintenance services; it renews every TTL/3 and stops its services as soon as a renewal fails.

e, err := elector.New(elector.Config{ClientID: id, Store: store, Clock: clk, Services: runMaintenance})
go e.Run(ctx)

Index

Constants

View Source
const (
	DefaultName           = "default"
	DefaultTTL            = 15 * time.Second
	DefaultReleaseTimeout = 5 * time.Second
)

Defaults.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Name     string
	ClientID string
	// TTL is the lease duration; the lease is renewed every TTL/3.
	TTL   time.Duration
	Store job.LeaseStore
	Clock clock.Clock
	// Services runs the leader-only services until its context is cancelled.
	Services func(ctx context.Context)
	// OnChange is called with true when leadership is gained and false when lost.
	OnChange       func(leader bool)
	Logger         *slog.Logger
	ReleaseTimeout time.Duration
}

Config configures an Elector.

type Elector

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

Elector holds a lease row to elect one leader per cluster. Clients that are not leader try to acquire the lease every TTL/3 (and on Wake); the leader renews it every TTL/3 and stops its services immediately when a renewal fails. On shutdown the lease is released so another client can take over at once.

func New

func New(cfg Config) (*Elector, error)

New validates cfg and applies defaults.

func (*Elector) Crash

func (e *Elector) Crash()

Crash simulates a leader crash for tests (crash seam): it stops the leader services immediately and abandons the lease without resigning, so other clients take over only after it expires. Run returns. Crash waits until Run has stopped; it is a no-op if Run was never started.

func (*Elector) Interval

func (e *Elector) Interval() time.Duration

Interval is the renew and acquisition period, TTL/3.

func (*Elector) IsLeader

func (e *Elector) IsLeader() bool

IsLeader reports whether this client currently holds the lease.

func (*Elector) Run

func (e *Elector) Run(ctx context.Context) error

Run campaigns until ctx is cancelled, then resigns.

func (*Elector) Wake

func (e *Elector) Wake()

Wake triggers an acquisition attempt now (another client resigned).

Jump to

Keyboard shortcuts

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