Documentation
¶
Overview ¶
Package lease is the serverless operator's process membership and unit ownership: one heartbeat row per process, fair-share claiming of (tenant, shard) 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 ¶
- type Config
- type Hooks
- type Leaser
- func (s *Leaser) DeleteProcess(ctx context.Context) error
- func (s *Leaser) Heartbeat(ctx context.Context) error
- func (s *Leaser) Owned() []Unit
- func (s *Leaser) Ready() bool
- func (s *Leaser) Release(ctx context.Context) ([]Unit, error)
- func (s *Leaser) Run(ctx context.Context) error
- func (s *Leaser) Tick(ctx context.Context) error
- type Reconciler
- type Unit
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. 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 ¶
DeleteProcess removes the process row, the last step of a graceful shutdown.
func (*Leaser) Heartbeat ¶
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) Release ¶
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 ¶
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.
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 in-flight deliveries so shedding can prefer idle units.