election

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2026 License: PostgreSQL Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Provide

func Provide(i *do.Injector)

Types

type Candidate

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

Candidate participates in a single named election. Once elected, candidate maintains leadership until its Stop method is called or until it fails to renew its leadership claim.

func NewCandidate

func NewCandidate(
	store *ElectionStore,
	loggerFactory *logging.Factory,
	electionName Name,
	candidateID string,
	ttl time.Duration,
	onClaim []ClaimHandler,
) *Candidate

NewCandidate creates a new Candidate instance to participate in the specified election. The candidateID uniquely identifies this participant. The ttl determines how long a leadership claim remains valid without renewal. The onClaim handlers are invoked when this candidate successfully claims leadership.

func (*Candidate) AddHandlers

func (c *Candidate) AddHandlers(handlers ...ClaimHandler)

func (*Candidate) Error

func (c *Candidate) Error() <-chan error

Error returns a channel that receives errors encountered during election operations such as claim attempts, renewals, or watch failures.

func (*Candidate) IsLeader

func (c *Candidate) IsLeader() bool

IsLeader returns true if this candidate currently holds leadership.

func (*Candidate) Start

func (c *Candidate) Start(ctx context.Context) error

Start begins participating in the election. It synchronously attempts to claim leadership and starts an asynchronous process to periodically refresh its claim or re-attempt to claim leadership. Start is idempotent.

func (*Candidate) Stop

func (c *Candidate) Stop(ctx context.Context) error

Stop ceases participation in the election, releases leadership if held, and stops the asynchronous renewal process. Stop is idempotent.

type ClaimHandler

type ClaimHandler func(ctx context.Context)

ClaimHandler is a callback function invoked when a candidate successfully claims leadership. Handlers are executed in separate goroutines.

type ElectionStore

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

func NewElectionStore

func NewElectionStore(client *clientv3.Client, root string) *ElectionStore

func (*ElectionStore) Create

func (*ElectionStore) Delete

func (*ElectionStore) GetByKey

func (s *ElectionStore) GetByKey(name Name) storage.GetOp[*StoredElection]

func (*ElectionStore) Key

func (s *ElectionStore) Key(name Name) string

func (*ElectionStore) Update

func (*ElectionStore) Watch

func (s *ElectionStore) Watch(name Name) storage.WatchOp[*StoredElection]

type Name

type Name string

func (Name) String

func (n Name) String() string

type Service

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

Service manages election operations.

func NewService

func NewService(
	store *ElectionStore,
	loggerFactory *logging.Factory,
) *Service

NewService returns a new Service.

func (*Service) NewCandidate

func (s *Service) NewCandidate(electionName Name, candidateID string, ttl time.Duration, onClaim ...ClaimHandler) *Candidate

NewCandidate creates a new Candidate for the given election. candidateID must be unique amongst candidates.

type StoredElection

type StoredElection struct {
	storage.StoredValue
	Name      Name      `json:"name"`
	LeaderID  string    `json:"leader_id"`
	CreatedAt time.Time `json:"created_at"`
}

Jump to

Keyboard shortcuts

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