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 (*Elector) Run ¶
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") },
)