runlock

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: MIT Imports: 3 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package runlock makes periodic background jobs (recommendation training, blob GC) run on exactly one instance at a time when several share a Postgres database.

Index

Constants

View Source
const (
	KeyRecTrainer int64 = 0x4c425a_0001 // "LBZ" + 1
	KeyBlobGC     int64 = 0x4c425a_0002
)

Lock keys for pg advisory locks. Arbitrary but fixed; distinct per job so unrelated jobs don't block each other.

Variables

View Source
var ErrBusy = errors.New("runlock: another instance is running this job")

ErrBusy means another instance holds the lock; the caller should skip this run rather than wait.

Functions

func Exclusive

func Exclusive(ctx context.Context, db *gorm.DB, key int64, fn func(tx *gorm.DB) error) error

Exclusive runs fn inside a transaction that holds the cluster-wide lock for key, or returns ErrBusy without running fn if another instance holds it.

On Postgres this is pg_try_advisory_xact_lock: the lock is tied to the transaction (released on commit/rollback), so it is safe through pgbouncer's transaction pooling — unlike a session-level advisory lock, which would be stranded on whatever server connection pgbouncer handed out. On sqlite (tests, single-process dev) there is only one process, so fn simply runs in a transaction.

Types

This section is empty.

Jump to

Keyboard shortcuts

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