leader

package
v0.0.0-...-eaf5e47 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package leader implements single-node leader election via PostgreSQL session-level advisory locks. Only one Kronos node holds the lock at a time; the others block in the election loop. When the leader's connection drops or context is cancelled, PostgreSQL releases the lock automatically and one waiting node takes over.

Advisory locks are a lightweight alternative to etcd/ZooKeeper for deployments already using Postgres. The tradeoff is that split-brain is bounded by the TCP keepalive timeout rather than a lease TTL — acceptable for job schedulers where a few seconds of overlap is recoverable via idempotent handlers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Elector

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

Elector manages leader election for a single Kronos node.

func New

func New(dsn string, log zerolog.Logger) *Elector

New creates an Elector. dsn is a standard PostgreSQL connection string.

func (*Elector) Run

func (e *Elector) Run(ctx context.Context, onElected func(context.Context), onRevoked func())

Run blocks until ctx is cancelled. It repeatedly tries to acquire the advisory lock. Once acquired, it calls onElected with a derived context and blocks until leadership is lost (connection failure or ctx cancel). onRevoked is called after leadership ends, before retrying.

Typical usage in main.go:

go elector.Run(ctx,
    func(leaderCtx context.Context) { scheduler.Run(leaderCtx) },
    func() { log.Info().Msg("lost leadership, standing by") },
)

Jump to

Keyboard shortcuts

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