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

election

package
v1.8.0 Latest Latest
Warning

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

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

Documentation

Overview

Package election provides a generic interface for election of a leader.

Index

Constants

View Source
const (
	MinPreElectionPause   = 10 * time.Millisecond
	MinMasterHoldInterval = 10 * time.Second
)

Minimum values for configuration intervals. TODO(pavelkalinnikov): These parameters are specific to the application, so shouldn't be here.

Variables

This section is empty.

Functions

func HeldInfo

func HeldInfo(held []string, ids []string) string

HeldInfo produces a textual description of the set of held IDs, compared to a complete set of IDs.

Types

type MasterTracker

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

MasterTracker tracks the current mastership state across multiple IDs.

func NewMasterTracker

func NewMasterTracker(ids []string, notify func(id string, isMaster bool)) *MasterTracker

NewMasterTracker creates a new MasterTracker instance to track the mastership status for the given set of IDs.

func (*MasterTracker) Count

func (mt *MasterTracker) Count() int

Count returns the number of IDs for which we are currently master.

func (*MasterTracker) Held

func (mt *MasterTracker) Held() []string

Held returns a (sorted) list of the IDs for which we are currently master.

func (*MasterTracker) IDs

func (mt *MasterTracker) IDs() []string

IDs returns a (sorted) list of the IDs that we are currently tracking.

func (*MasterTracker) Set

func (mt *MasterTracker) Set(id string, isMaster bool)

Set changes the tracked mastership status for the given ID. This method should be called exactly once for each state transition.

func (*MasterTracker) String

func (mt *MasterTracker) String() string

String returns a textual decription of the current mastership status.

type Resignation

type Resignation struct {
	ID string
	// contains filtered or unexported fields
}

Resignation indicates that a master should explicitly resign mastership, and call the Execute() method as soon as no master-related activity is ongoing.

func (*Resignation) Execute

func (r *Resignation) Execute(ctx context.Context)

Execute performs the pending deliberate resignation for an election runner.

type Runner

type Runner struct {
	// Allow the user to store a Cancel function with the runner for convenience.
	Cancel context.CancelFunc
	// contains filtered or unexported fields
}

Runner controls a continuous election process.

func NewRunner

func NewRunner(id string, cfg *RunnerConfig, tracker *MasterTracker, cancel context.CancelFunc, el election2.Election) *Runner

NewRunner builds a new election Runner instance with the given config. On calling Run(), the provided Election will be continuously monitored, and mastership changes will be notified to the provided MasterTracker instance.

func (*Runner) Run

func (er *Runner) Run(ctx context.Context, pending chan<- Resignation)

Run performs a continuous election process. It runs continuously until the context is canceled or an internal error is encountered.

type RunnerConfig

type RunnerConfig struct {
	// PreElectionPause is the maximum interval to wait before starting a
	// mastership election for a particular log.
	PreElectionPause time.Duration
	// MasterHoldInterval is the minimum interval to hold mastership for.
	MasterHoldInterval time.Duration
	// MasterHoldJitter is the maximum addition to MasterHoldInterval.
	MasterHoldJitter time.Duration

	TimeSource clock.TimeSource
}

RunnerConfig describes the parameters for an election Runner.

func (*RunnerConfig) ResignDelay added in v1.3.0

func (cfg *RunnerConfig) ResignDelay() time.Duration

ResignDelay returns a randomized delay of how long to keep mastership for.

Jump to

Keyboard shortcuts

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