election

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Backoff

type Backoff interface {
	// Duration returns the time to sleep for the nth invocation
	Duration(n int) time.Duration
}

Backoff controls the interval of campaigns

type Election

type Election interface {
	Start(ctx context.Context) error
	Stop()
}

Election manages leader elections

func NewElection

func NewElection(name string, key string, bucket nats.KeyValue, opts ...Option) (Election, error)

type Option

type Option func(o *options)

Option configures the election system

func OnCampaign

func OnCampaign(cb func(s State)) Option

OnCampaign is called each time a campaign is done by the leader or a candidate

func OnLost

func OnLost(cb func()) Option

OnLost is a callback called when losing an election

func OnWon

func OnWon(cb func()) Option

OnWon is a callback called when winning an election

func WithBackoff

func WithBackoff(bo Backoff) Option

WithBackoff will use the provided Backoff timer source to decrease campaign intervals over time

func WithDebug

func WithDebug(cb func(format string, a ...any)) Option

WithDebug sets a function to do debug logging with

type State

type State uint

State indicates the current state of the election

const (
	// UnknownState indicates the state is unknown, like when the election is not started
	UnknownState State = 0
	// CandidateState is a campaigner that is not the leader
	CandidateState State = 1
	// LeaderState is the leader
	LeaderState State = 2
)

func (State) String

func (s State) String() string

Jump to

Keyboard shortcuts

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