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

singleton

package
v0.40.18 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package singleton runs a periodic job on one replica at a time. The replica that takes the job's Postgres advisory lock runs it; one that finds the lock held skips that tick (counted and logged at debug), and the next tick catches up, since every job that uses this is idempotent. The lock is session-level on a dedicated connection, so a pod that dies releases it with its connection.

Index

Constants

View Source
const (
	Ran     = "ran"
	Skipped = "skipped" // another replica held the lock
	Errored = "error"   // the lock could not be tried (no connection, database error)
)

Outcomes, as the metric labels them.

Variables

This section is empty.

Functions

func Acquire

func Acquire(ctx context.Context, d *sql.DB, job string, key Key) (release func(), ok bool, err error)

Acquire takes job's lock without waiting. When it returns ok, the caller holds the lock until it calls release (on a connection kept for the purpose; a pod that dies releases it with the connection). A held lock is (nil, false, nil), counted as skipped; a fault trying it is (nil, false, err).

func Run

func Run(ctx context.Context, d *sql.DB, job string, key Key, fn func(ctx context.Context) error) (bool, error)

Run is Acquire around fn: it runs fn under job's lock and reports whether it ran. The lock is released when fn returns, however it ends.

func SetObserver

func SetObserver(f func(job, outcome string))

SetObserver tells f every attempt's outcome (job, outcome), for rivolt_singleton_runs_total. Set once at boot.

Types

type Key

type Key struct {
	Name string
	ID   int64
}

Key names a job's lock: Name is hashed by Postgres; ID is used as is, for a job whose lock existed before this package (its old pods must keep excluding the new ones during a rollout). Name wins when both are set.

Jump to

Keyboard shortcuts

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