election

package
v0.0.0-...-dd72184 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	StateUnknown = iota
	StateFollower
	StateLeader
)

Possible values for ElectionState.State.

Variables

This section is empty.

Functions

func Observe

func Observe(ctx context.Context, session *concurrency.Session, path string) <-chan ElectionState

Observe an election without participating in it. It's a simple way to track the current leader.

func WaitForever

func WaitForever(ctx context.Context) error

WaitForever is an Op that does nothing (until the Context is canceled).

Types

type Election

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

Election manages (or witnesses) an election protocol on a given database prefix.

func New

func New(session *concurrency.Session, path string, self *pb.Endpoint) *Election

New creates a new Election, scoped within the provided session. If self is not nil, the caller participates in the election and can call Run().

func (*Election) Run

func (e *Election) Run(ctx context.Context, op Op) error

Run the election as a participant, and run op when we're the leader.

func (*Election) Watch

func (e *Election) Watch(ctx context.Context) <-chan ElectionState

Watch the election and get notified of state changes.

type ElectionState

type ElectionState struct {
	State  int
	Leader *pb.Endpoint
}

ElectionState represents the current state of the election, with the value of the current leader endpoint, if any.

func (ElectionState) String

func (s ElectionState) String() string

type ElectionWatcher

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

An ElectionWatcher simply consumes the output of Election.Watch and stores the most recent result so you can query it asynchronously.

func NewWatcher

func NewWatcher(ctx context.Context, el *Election) *ElectionWatcher

NewWatcher creates a new ElectionWatcher.

func (*ElectionWatcher) Notify

func (w *ElectionWatcher) Notify() <-chan struct{}

Notify returns a channel that is triggered on every state change.

func (*ElectionWatcher) State

func (w *ElectionWatcher) State() ElectionState

State returns the most recent ElectionState.

type Op

type Op func(context.Context) error

Op is a function that will be called when a node participating in an election becomes the leader. Leadership is resigned once the function exits (or is canceled via the Context).

Jump to

Keyboard shortcuts

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