postgreslock

package
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package postgreslock is a table-based distributed lock that satisfies lock.Provider. Opt-in: importing this package pulls in pgx/v5; the root lock package has no driver dependency.

pg_try_advisory_lock would be faster, but advisory locks have no TTL — a crashed holder keeps the lock until its session ends. With table-based locks the TTL is explicit and enforced by the upsert's WHERE clause: another holder can reclaim an expired row.

Acquire is a single INSERT … ON CONFLICT … DO UPDATE … WHERE … RETURNING statement — atomic in Postgres, so no race window between checking and taking.

Index

Constants

View Source
const CreateTableSQL = `` /* 205-byte string literal not displayed */

CreateTableSQL is the schema for the lock table — substitute {table} for the actual name.

View Source
const DefaultTable = "fc_locks"

DefaultTable is the lock table name when New is used.

Variables

This section is empty.

Functions

func InitSchema

func InitSchema(ctx context.Context, pool *pgxpool.Pool) error

InitSchema creates DefaultTable. Safe to run repeatedly.

func InitSchemaWithTable

func InitSchemaWithTable(ctx context.Context, pool *pgxpool.Pool, table string) error

InitSchemaWithTable creates the lock table with a custom name.

Types

type Provider

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

Provider is a Postgres-backed implementation of lock.Provider.

Each Acquire mints a unique holder token (random UUID) so Release only deletes locks the caller actually owns — protects against accidental release of a lock that has since been reclaimed by another holder.

func New

func New(pool *pgxpool.Pool) *Provider

New builds a provider against pool using DefaultTable.

func WithTable

func WithTable(pool *pgxpool.Pool, table string) *Provider

WithTable overrides the table name.

func (*Provider) Acquire

func (p *Provider) Acquire(ctx context.Context, key string, ttl time.Duration) (lock.Handle, error)

Acquire — see lock.Provider.Acquire.

func (*Provider) ReapExpired

func (p *Provider) ReapExpired(ctx context.Context) (int64, error)

ReapExpired removes rows whose TTL has elapsed without being released and returns the number removed. Optional — Acquire reclaims expired rows implicitly via the upsert.

Jump to

Keyboard shortcuts

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