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 ¶
- func EnsureSchema(db *gorm.DB) error
- func InstanceID() string
- func Release(ctx context.Context, db *gorm.DB, name, holder string, now time.Time) error
- func TryAcquire(ctx context.Context, db *gorm.DB, name, holder string, ttl time.Duration, ...) (bool, error)
- func WritePrometheus(body *strings.Builder)
- type Elector
- type Record
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 ¶
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 ¶
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.
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.