lease

package
v4.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package lease provides database-backed leases so that work which must run in exactly one Control process (periodic resets, stats collection, the forward job executors) stays single even when several processes share one database: during a rolling update, or with more than one replica.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EnsureSchema

func EnsureSchema(db *gorm.DB) error

EnsureSchema creates the lease table.

func InstanceID

func InstanceID() string

InstanceID identifies this process as a lease holder: the host name (the pod name on Kubernetes) plus a random suffix, so a restarted process never inherits a lease it did not renew.

func Release

func Release(ctx context.Context, db *gorm.DB, name, holder string, now time.Time) error

Release gives the lease up early if holder still owns it, so another process can take over without waiting for the expiry.

func TryAcquire

func TryAcquire(ctx context.Context, db *gorm.DB, name, holder string, ttl time.Duration, now time.Time) (bool, error)

TryAcquire takes or renews the lease for holder when it is free, expired, or already held by holder. It reports whether holder owns the lease now.

func WritePrometheus

func WritePrometheus(body *strings.Builder)

WritePrometheus writes anixops_lease_leader: 1 for each lease this process currently holds, 0 for leases it is waiting for.

Types

type Elector

type Elector struct {
	DB     *gorm.DB
	Name   string
	Holder string
	// TTL is the lease duration; renewals happen every TTL/3. A leader that
	// cannot renew stops its work after 2/3 of the TTL, before another process
	// can take the lease over.
	TTL time.Duration
	Now func() time.Time
	// contains filtered or unexported fields
}

Elector runs work only while this process holds a named lease.

func (*Elector) IsLeader

func (e *Elector) IsLeader() bool

IsLeader reports whether the elector currently runs its work.

func (*Elector) Run

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

Run blocks until ctx is done. Whenever the process holds the lease it runs work with a context that is cancelled as soon as leadership ends, and it waits for work to return before trying to lead again.

type Record

type Record struct {
	Name       string    `gorm:"primaryKey;size:100"`
	Holder     string    `gorm:"size:200;not null;default:''"`
	ExpiresAt  time.Time `gorm:"not null;index"`
	AcquiredAt time.Time
	RenewedAt  time.Time
}

Record is one named lease. Holder owns it until ExpiresAt; the holder renews it well before then.

func (Record) TableName

func (Record) TableName() string

TableName keeps the lease table with the other kernel tables.

Jump to

Keyboard shortcuts

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