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 ¶
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 ¶
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 ¶
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.