oauthlock

package
v0.0.0-...-2ed2b13 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrLockBusy is returned when pg_try_advisory_xact_lock returns false —
	// another transaction holds the lock for this integration.
	ErrLockBusy = errors.New("oauthlock: lock busy")

	// ErrLockExhausted is returned by RefreshWithRetry when all backoff
	// attempts are exhausted without acquiring the lock.
	ErrLockExhausted = errors.New("oauthlock: retries exhausted")
)
View Source
var ContendedTotal = promauto.NewCounterVec(prometheus.CounterOpts{
	Name: "oauth_refresh_lock_contended_total",
	Help: "OAuth refresh attempts that failed to acquire the advisory lock and returned ErrLockBusy.",
}, []string{"platform"})

ContendedTotal counts OAuth refresh attempts that failed to acquire the advisory lock and returned ErrLockBusy.

View Source
var ExhaustedTotal = promauto.NewCounterVec(prometheus.CounterOpts{
	Name: "oauth_refresh_lock_exhausted_total",
	Help: "OAuth refresh attempts that exhausted all backoff retries without acquiring the advisory lock.",
}, []string{"platform"})

ExhaustedTotal counts OAuth refresh attempts that exhausted all backoff retries without acquiring the advisory lock.

Functions

func RefreshWithRetry

func RefreshWithRetry(
	ctx context.Context,
	pool LockExecutor,
	integrationID uuid.UUID,
	platform string,
	fn func(context.Context, pgx.Tx) error,
) error

RefreshWithRetry retries WithRefreshLock with exponential backoff on ErrLockBusy. Returns ErrLockExhausted after all slots are exhausted.

func RefreshWithRetryFn

func RefreshWithRetryFn(
	ctx context.Context,
	pool LockExecutor,
	integrationID uuid.UUID,
	platform string,
	fn func(context.Context, pgx.Tx) error,
	lockFn func(context.Context, LockExecutor, uuid.UUID, string, func(context.Context, pgx.Tx) error) error,
) error

RefreshWithRetryFn is RefreshWithRetry with an injectable lock function for testing. Production callers should use RefreshWithRetry.

func RegisterMetrics

func RegisterMetrics(_ interface{})

RegisterMetrics is a no-op for the promauto-registered counters; it exists so tests can call it to verify the package is importable.

func WithRefreshLock

func WithRefreshLock(
	ctx context.Context,
	pool LockExecutor,
	integrationID uuid.UUID,
	platform string,
	fn func(context.Context, pgx.Tx) error,
) error

WithRefreshLock opens a transaction, tries to acquire pg_try_advisory_xact_lock keyed by integrationID, and calls fn inside the transaction. The lock is released automatically on commit or rollback. Returns ErrLockBusy immediately when the lock is held by another connection.

Types

type LockExecutor

type LockExecutor interface {
	BeginTx(ctx context.Context, opts pgx.TxOptions) (pgx.Tx, error)
}

LockExecutor is satisfied by *pgxpool.Pool and pgxmock.PgxPoolIface.

Jump to

Keyboard shortcuts

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