postgres

package
v0.7.0-rc1 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotPostgreSQL is returned when attempting to use PostgreSQL advisory locks
	// with a non-PostgreSQL database.
	ErrNotPostgreSQL = errors.New("database is not PostgreSQL; advisory locks are only supported on PostgreSQL")

	// ErrDatabaseConnectionFailed is returned when the database connection fails.
	ErrDatabaseConnectionFailed = errors.New("failed to connect to database")

	// ErrCircuitBreakerOpen is returned when the circuit breaker is open due to
	// repeated failures, preventing further lock operations.
	ErrCircuitBreakerOpen = errors.New("circuit breaker open: database is unavailable")

	// ErrLockAcquisitionFailed is returned when lock acquisition fails after all retries.
	ErrLockAcquisitionFailed = errors.New("failed to acquire lock after retries")

	// ErrLockContention is returned when a lock is already held by another process.
	ErrLockContention = errors.New("lock held by another process")

	// ErrNoDatabase is returned when no database connection is provided.
	ErrNoDatabase = errors.New("no database connection provided")
)

Functions

func NewRWLocker

func NewRWLocker(
	ctx context.Context,
	querier database.Querier,
	cfg Config,
	retryCfg lock.RetryConfig,
	allowDegradedMode bool,
) (lock.RWLocker, error)

NewRWLocker creates a new PostgreSQL advisory lock-based read-write locker.

Types

type Config

type Config struct {
	// KeyPrefix is prepended to all lock keys for namespacing.
	// Defaults to "ncps:lock:" if empty.
	KeyPrefix string
}

Config holds the configuration for PostgreSQL advisory locks.

type Locker

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

Locker implements lock.Locker using PostgreSQL advisory locks.

func NewLocker

func NewLocker(
	ctx context.Context,
	querier database.Querier,
	cfg Config,
	retryCfg lock.RetryConfig,
	allowDegradedMode bool,
) (*Locker, error)

NewLocker creates a new PostgreSQL advisory lock-based locker.

func (*Locker) Lock

func (l *Locker) Lock(ctx context.Context, key string, ttl time.Duration) error

Lock acquires an exclusive lock with retry and exponential backoff. NOTE: The `ttl` parameter is ignored. The lock is held until Unlock() is called or the underlying database connection is closed.

func (*Locker) TryLock

func (l *Locker) TryLock(ctx context.Context, key string, ttl time.Duration) (bool, error)

TryLock attempts to acquire an exclusive lock without retries. NOTE: The `ttl` parameter is ignored. The lock is held until Unlock() is called or the underlying database connection is closed.

func (*Locker) Unlock

func (l *Locker) Unlock(ctx context.Context, key string) error

Unlock releases an exclusive lock.

type RWLocker

type RWLocker struct {
	*Locker
	// contains filtered or unexported fields
}

RWLocker implements lock.RWLocker using PostgreSQL advisory locks. It provides both exclusive (write) and shared (read) locking semantics.

func (*RWLocker) RLock

func (rw *RWLocker) RLock(ctx context.Context, key string, ttl time.Duration) error

RLock acquires a shared read lock. Multiple readers can hold the lock simultaneously, but they will block if a writer holds the lock. NOTE: The `ttl` parameter is ignored. The lock is held until RUnlock() is called or the underlying database connection is closed.

func (*RWLocker) RUnlock

func (rw *RWLocker) RUnlock(ctx context.Context, key string) error

RUnlock releases a shared read lock.

Jump to

Keyboard shortcuts

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