leader

package
v0.3.13 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package leader provides distributed leader election with PostgreSQL-based locking.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LeaderElector

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

LeaderElector manages leader election for distributed services using pglock to coordinate leadership across multiple instances.

func NewLeaderElector

func NewLeaderElector(
	gormDB *gorm.DB,
	ctx context.Context,
	lockName string,
	lockDuration time.Duration,
	heartbeatFreq time.Duration,
) (*LeaderElector, error)

NewLeaderElector creates a new LeaderElector instance.

Parameters:

  • gormDB: PostgreSQL database connection
  • ctx: Base context for leadership contexts
  • lockName: Unique identifier for the distributed lock
  • lockDuration: How long the lock is held before expiring
  • heartbeatFreq: How often to renew the lock while leader

Returns a configured LeaderElector, or an error if client creation fails. Use OnBecomeLeader to register callbacks that will be invoked when leadership is acquired.

func (*LeaderElector) Healthy added in v0.3.10

func (e *LeaderElector) Healthy() bool

Healthy reports whether this elector can reach the database. Returns false before first DB contact (cold start) and when consecutive infrastructure failures exceed the threshold.

func (*LeaderElector) OnBecomeLeader

func (e *LeaderElector) OnBecomeLeader(callback func(context.Context))

OnBecomeLeader registers a callback to be invoked when this instance becomes leader. The callback receives a context that is canceled when leadership is lost. The callback should block until the context is canceled for graceful shutdown. Multiple callbacks can be registered and will be executed concurrently.

If this instance is already the leader when this method is called, the callback will be invoked immediately with the current leadership context.

func (*LeaderElector) Wait

func (e *LeaderElector) Wait() error

Wait blocks until the background goroutine exits and returns any error. This replaces the old Run() method in the new API.

Jump to

Keyboard shortcuts

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