Documentation
¶
Overview ¶
Package postgres provides a lease backed by PostgreSQL.
Index ¶
Constants ¶
const CreateLockTableSQL = `` /* 279-byte string literal not displayed */
CreateLockTableSQL is the SQL statement to create the lease table.
const DefaultLeaseTTL = 30 * time.Second
DefaultLeaseTTL is the default TTL for a lease.
Variables ¶
var ErrConflict = errors.New("lease is held by another owner")
ErrConflict is returned when the lease is held by another owner or doesn't exist.
Functions ¶
This section is empty.
Types ¶
type Lease ¶
type Lease struct {
// Key is the lease identifier.
Key string
// FencingToken is a unique token used to prevent conflicts when releasing or renewing the lease.
FencingToken string
// TTL is the time-to-live duration for the lease.
TTL time.Duration
// contains filtered or unexported fields
}
Lease represents a distributed lease on a key.
func (*Lease) Lock ¶
Lock acquires the lease, blocking until the lock is available or the context is canceled.
func (*Lease) Renew ¶
Renew renews the lease by extending the TTL. Returns ErrConflict if the lease is not held by this fencing token.
func (*Lease) TryLock ¶
TryLock attempts to acquire the lease without blocking. Returns ErrConflict if the lease is held by another owner.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager manages distributed leases using PostgreSQL. It provides methods to create and manage leases across multiple nodes.
func NewManager ¶
NewManager creates a new Manager with the given PostgreSQL connection pool. The node ID is automatically generated from the hostname and process ID. It also starts a background goroutine to clean up expired locks.
func (*Manager) Lease ¶
Lease creates a new Lease for the given key with the default configuration. The lease is not acquired until Lease.Lock or Lease.TryLock is called on it.