Help us shape the future of Go! Take the 2026 Go Developer Survey

pglock

package
v0.28.0 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package pglock is a payforge.Locker that holds across instances, with a PostgreSQL transaction-scoped advisory lock.

Lock opens a transaction on a connection of its own and takes pg_advisory_xact_lock on a 64-bit hash of the key; unlock rolls the transaction back, which releases the lock. The work under the lock runs on other connections; the transaction exists only to hold the lock. Because the lock belongs to a transaction rather than a session, it cannot outlive its connection: a connection that breaks releases it, and it is never left held on a connection returned to the pool.

Set the same Locker on payforge.Lifecycle.Locker, the payforge Stripe client's Config.Locker and billing's configs, so webhooks and checkouts for one organization exclude each other. The lock is not reentrant: never take a key while already holding it.

It is PostgreSQL only, which is why it is a package of its own: the rest of billing runs on any Ent dialect.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Locker

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

Locker implements payforge.Locker on PostgreSQL.

func New

func New(db *sql.DB, logger *slog.Logger) *Locker

New returns a Locker on db, which must be PostgreSQL. A failure to release a lock, which the database also releases when the connection ends, is logged to logger, or slog.Default() when it is nil. New panics on a nil db, a programming error.

Each held lock uses one connection from db's pool until it is released, so size the pool for the concurrent webhook deliveries and checkouts plus the work they do.

func (*Locker) Lock

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

Lock implements payforge.Locker. Waiting ends with ctx; once held, the lock lasts until unlock is called, whatever happens to ctx.

Jump to

Keyboard shortcuts

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