postgres

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package postgres provides a lease backed by PostgreSQL.

Index

Constants

View Source
const CreateLockTableSQL = `` /* 279-byte string literal not displayed */

CreateLockTableSQL is the SQL statement to create the lease table.

View Source
const DefaultLeaseTTL = 30 * time.Second

DefaultLeaseTTL is the default TTL for a lease.

Variables

View Source
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) Expiry

func (l *Lease) Expiry(ctx context.Context) (time.Duration, error)

Expiry returns the remaining TTL of the lease.

func (*Lease) Lock

func (l *Lease) Lock(ctx context.Context) error

Lock acquires the lease, blocking until the lock is available or the context is canceled.

func (*Lease) Renew

func (l *Lease) Renew(ctx context.Context) error

Renew renews the lease by extending the TTL. Returns ErrConflict if the lease is not held by this fencing token.

func (*Lease) TryLock

func (l *Lease) TryLock(ctx context.Context) error

TryLock attempts to acquire the lease without blocking. Returns ErrConflict if the lease is held by another owner.

func (*Lease) Unlock

func (l *Lease) Unlock(ctx context.Context) error

Unlock releases the lease. Returns ErrConflict if the lease is not held by this fencing token.

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

func NewManager(pool *pgxpool.Pool) *Manager

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

func (m *Manager) Lease(key string) *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.

Jump to

Keyboard shortcuts

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