leader

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2026 License: MIT Imports: 11 Imported by: 0

README

internal/leader

Logic overview

The leader package provides leadership-event orchestration primitives used to drive leader/follower runtime behavior.

  • Elector is the abstraction for any leader-election backend (Kubernetes Lease, static mode, test feed).
  • Runner converts election callbacks into role and error channels for consumers like runtime role managers.
  • Role transitions are deduplicated to avoid repeated leader -> leader or follower -> follower churn.
  • Context cancellation is treated as normal shutdown and not surfaced as an error.
  • KubernetesLeaseElector performs lease create/renew/takeover decisions directly through Kubernetes coordination API Lease resources.

API reference

Types
  • Elector: backend contract that emits role transitions.
  • Runner: wraps an Elector and exposes role/error channels.
  • StaticElector: emits a fixed role and waits for context cancellation.
  • ChannelElector: emits role transitions from a channel.
  • KubernetesLeaseConfig: configuration for Lease API endpoint, identity, timing, and HTTP client behavior.
  • KubernetesLeaseElector: Lease-backed elector implementation.
Functions
  • NewRunner(elector Elector, logger ...*zap.Logger) *Runner: creates a runner with optional zap logger.
  • NewKubernetesLeaseElector(cfg KubernetesLeaseConfig) (*KubernetesLeaseElector, error): creates a Lease-backed elector.
Methods
  • (*Runner) Start(ctx context.Context) (<-chan bool, <-chan error): starts election observation and returns deduplicated role transitions plus fatal election errors.
  • (StaticElector) Run(ctx context.Context, emit func(isLeader bool)) error: emits the configured role once.
  • (ChannelElector) Run(ctx context.Context, emit func(isLeader bool)) error: relays events from Events until close or cancellation.
  • (*KubernetesLeaseElector) Run(ctx context.Context, emit func(isLeader bool)) error: periodically acquires/renews the configured Lease and emits leadership state.
  • (*KubernetesLeaseElector) TryAcquireOrRenew(ctx context.Context) (bool, error): executes one Lease acquisition/renewal decision.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ChannelElector

type ChannelElector struct {
	Events <-chan bool
	Err    error
}

ChannelElector emits role events from an input channel.

func (ChannelElector) Run

func (e ChannelElector) Run(ctx context.Context, emit func(isLeader bool)) error

Run emits role events from a channel until closed or context cancellation.

type Elector

type Elector interface {
	Run(ctx context.Context, emit func(isLeader bool)) error
}

Elector emits leadership transitions for a single process identity.

type KubernetesLeaseConfig

type KubernetesLeaseConfig struct {
	APIBaseURL string
	Namespace  string
	LeaseName  string
	Identity   string
	//nolint:gosec // Bearer token is required for Kubernetes API authentication.
	BearerToken   string
	LeaseDuration time.Duration
	RetryPeriod   time.Duration
	HTTPClient    *http.Client
	Now           func() time.Time
	Sleep         func(time.Duration)
}

KubernetesLeaseConfig configures Kubernetes Lease-based election.

type KubernetesLeaseElector

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

KubernetesLeaseElector implements leader election with coordination.k8s.io Lease resources.

func NewKubernetesLeaseElector

func NewKubernetesLeaseElector(cfg KubernetesLeaseConfig) (*KubernetesLeaseElector, error)

NewKubernetesLeaseElector creates a Kubernetes Lease elector.

func (*KubernetesLeaseElector) Run

func (e *KubernetesLeaseElector) Run(ctx context.Context, emit func(isLeader bool)) error

Run evaluates lease ownership in a retry loop and emits leadership state.

func (*KubernetesLeaseElector) TryAcquireOrRenew

func (e *KubernetesLeaseElector) TryAcquireOrRenew(ctx context.Context) (bool, error)

TryAcquireOrRenew attempts to create, renew, or take over the configured lease.

type Runner

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

Runner adapts an Elector into role and error channels used by runtime orchestration.

func NewRunner

func NewRunner(elector Elector, logger ...*zap.Logger) *Runner

NewRunner creates a leader-election runner.

func (*Runner) Start

func (r *Runner) Start(ctx context.Context) (<-chan bool, <-chan error)

Start begins election observation and returns role and error channels.

type StaticElector

type StaticElector struct {
	IsLeader bool
}

StaticElector emits a fixed role and then waits for context cancellation.

func (StaticElector) Run

func (e StaticElector) Run(ctx context.Context, emit func(isLeader bool)) error

Run emits the configured static role until context cancellation.

Jump to

Keyboard shortcuts

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