Documentation
¶
Overview ¶
Package lease grants mutual exclusion between processes through Postgres session advisory locks, all held on one connection per process.
Claims hijacks a single connection from the pool and takes every lock on it. Run keeps that connection alive and pings it every PingInterval. When the session ends, every held Lease is lost at once: its context is cancelled with cause ErrLost, and Run reconnects.
Leases are not fences. A lost lease is only noticed at the next ping, and in that window another process can acquire the same lease while the old holder is still working. Every holder must therefore fence its own writes (for example with a compare-and-set or a generation column); holding the lease only prevents duplicate work, it never guarantees correctness.
Locks live in the two-int advisory space (fnv32a(Namespace), fnv32a(Name)). PgBouncer in transaction mode is not supported.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrLost is the cause of a lease context cancelled because the lock // connection ended or Run stopped. ErrLost = errors.New("lease: lost") // ErrHeld is returned by TryAcquire when another session, or this // process, already holds the lock. ErrHeld = errors.New("lease: held") // ErrNotConnected is returned by TryAcquire while Run has no connection. ErrNotConnected = errors.New("lease: not connected") // ErrEmptyName is returned by TryAcquire for a Key without a Name. ErrEmptyName = errors.New("lease: empty name") // ErrRunning is returned by Run when it is already running. ErrRunning = errors.New("lease: already running") )
Functions ¶
This section is empty.
Types ¶
type Claims ¶
type Claims struct {
// contains filtered or unexported fields
}
Claims takes session advisory locks on one connection hijacked from the pool. Run must be running for TryAcquire to succeed.
func (*Claims) Run ¶
Run hijacks the lock connection, pings it every PingInterval and reconnects after it is lost. When ctx ends it closes the connection, cancels every held lease with ErrLost and returns nil.
func (*Claims) TryAcquire ¶
TryAcquire takes the lock for key without waiting. The returned context is derived from ctx and is cancelled with cause ErrLost when the lease is lost; Release cancels it too. It fails with ErrHeld when another session or this process holds the lock, with ErrNotConnected while Run has no connection, and with ErrEmptyName when key has no Name. A Key whose lock collides with one this process holds counts as held.
type Config ¶
type Config struct {
// PingInterval is how often the lock connection is checked. Lease loss
// is noticed at most this late, plus up to 5s for a ping that hangs; a
// slow ping is not a loss. Defaults to 5s.
PingInterval time.Duration
// Logger defaults to slog.Default().
Logger *slog.Logger
}
Config configures Claims.
type Key ¶
type Key struct {
// Namespace is the first half of the lock, hashed with fnv32a.
Namespace string
// Name is the second half of the lock, hashed with fnv32a. Required.
Name string
}
Key identifies a lock. Namespace groups related locks, such as all locks of one package in one schema.
type Lease ¶
type Lease struct {
// contains filtered or unexported fields
}
Lease is a held lock. Its holder must stop working when the context returned by TryAcquire ends.
type Option ¶
Option configures Claims beyond Config.
func WithMeterProvider ¶
func WithMeterProvider(mp metric.MeterProvider) Option
WithMeterProvider records metrics with mp instead of the global provider.