Help us shape the future of Go! Take the 2026 Go Developer Survey

lease

package
v0.113.1 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package lease is the serverless operator's process membership and unit ownership: one heartbeat row per process, fair-share claiming of (tenant, partition) units with FOR UPDATE SKIP LOCKED, shedding above fair share, and a periodic sweep of expired process rows. The pattern is pgoutbox's consumer session leasing with indexed candidate lookups instead of a table scan.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Hostname          string
	Version           string
	ProcessId         uuid.UUID
	TTL               time.Duration
	HeartbeatInterval time.Duration
	RebalanceInterval time.Duration
	SweepInterval     time.Duration
	SweepCutoff       time.Duration
	ShedHysteresis    float64

	// ClaimBatch is the most units one claim statement takes; MaxClaimPerTick caps how many a
	// tick claims in total and sizes the sample of the claimable population each tick counts.
	// The tick's budget is its fair share of the claimable units, so a takeover of many units
	// spreads evenly over the live processes and a single survivor takes at most
	// MaxClaimPerTick per tick.
	ClaimBatch      int32
	MaxClaimPerTick int32
}

Config is the leaser's timing and claim sizing. Zero values take the defaults below.

type Hooks

type Hooks struct {
	Claimed    func(n int)
	Shed       func(n int)
	Rebalanced func(d time.Duration)
	Owned      func(units, endpoints int)
}

Hooks are optional observers for metrics. Nil funcs are skipped.

type Leaser

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

Leaser owns the process row and the set of owned units. Tick and Heartbeat are exported so tests can drive one step at a time; Run loops them.

func New

func New(repo repository.ServerlessRepository, reconciler Reconciler, cfg Config, l *zerolog.Logger, hooks Hooks) *Leaser

func (*Leaser) DeleteProcess

func (s *Leaser) DeleteProcess(ctx context.Context) error

DeleteProcess removes the process row, the last step of a graceful shutdown.

func (*Leaser) Heartbeat

func (s *Leaser) Heartbeat(ctx context.Context) error

Heartbeat upserts the process row: the only steady-state write of a process. A heartbeat that lands more than the TTL after the previous one found the row expired in between; the lease table is re-read at once, since other processes may have taken units meanwhile.

func (*Leaser) Owned

func (s *Leaser) Owned() []Unit

Owned snapshots the owned units.

func (*Leaser) Ready

func (s *Leaser) Ready() bool

Ready reports whether the first rebalance tick has completed.

func (*Leaser) Release

func (s *Leaser) Release(ctx context.Context) ([]Unit, error)

Release gives every owned unit back in one statement and forgets them locally. The caller drains and closes registrations afterwards, then calls DeleteProcess.

func (*Leaser) Run

func (s *Leaser) Run(ctx context.Context) error

Run heartbeats until the process row is live, retrying with backoff so a database that is unreachable at startup delays the process instead of stopping it, then loops the heartbeat, rebalance and sweep until ctx is done. Loop errors are logged, not returned: a transient database error must not stop the process.

func (*Leaser) Tick

func (s *Leaser) Tick(ctx context.Context) error

Tick is one rebalance: refresh ownership from the lease table, compute the fair share by endpoint weight, claim up to the budget from unowned and dead-process units, shed above fair share plus hysteresis, then hand the ownership diff to the reconciler.

type Reconciler

type Reconciler interface {
	UnitsGained(ctx context.Context, units []Unit)
	UnitsLost(ctx context.Context, units []Unit)
	InFlight(unit Unit) int
}

Reconciler is what the leaser drives: the runner opens and closes registrations and pollers as ownership changes and reports the deliveries losing a unit would interrupt, so shedding takes idle units first.

type Unit

Unit is one lease unit.

Jump to

Keyboard shortcuts

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