singleton

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2026 License: MIT Imports: 4 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 singleton implements a distributed lock over NATS KV - so that a thrall with scope="singleton" runs only once across the whole cluster, even when several lords run against it (multi-node). The lock rests on two primitives:

  • kv.Create = atomic "create only if it does not exist" (CAS on non-existence) -> acquire
  • bucket TTL = when the holder stops renewing (crashed), the key expires -> failover

Index

Constants

View Source
const (
	Bucket = "aether_singletons"
	// TTL is the lock's time-to-live: the key expires when the holder stops renewing it,
	// which triggers failover. The thrall derives its fencing lease window from the same value.
	TTL = 3 * time.Second
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Lock

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

Lock is a held lock for a single name; it keeps the revision for CAS renewal.

func (*Lock) Epoch

func (l *Lock) Epoch() uint64

Epoch returns the fencing token of this held lock. It is injected into the singleton thrall so the thrall can verify, independently of the lord, that it still holds the lock.

func (*Lock) Release

func (l *Lock) Release() error

Release releases the lock (immediately, without waiting for the TTL).

func (*Lock) Renew

func (l *Lock) Renew() error

Renew renews the lock (CAS on the current revision) and resets the TTL. An error = the lock was lost.

type Manager

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

Manager opens the KV bucket of locks.

func Open

func Open(nc *nats.Conn) (*Manager, error)

Open opens/creates the bucket of locks (with TTL).

func (*Manager) TryAcquire

func (m *Manager) TryAcquire(key, holder string) (*Lock, bool, error)

TryAcquire attempts to acquire the lock atomically. ok=false means another lord holds it. The create revision doubles as the fencing epoch handed out to the thrall.

func (*Manager) Verify

func (m *Manager) Verify(key string, epoch uint64) (ok bool, err error)

Verify reports whether the lock key still carries the given fencing epoch. ok=false with a nil error means the epoch was superseded or the key is gone (lock lost); a non-nil error means the KV could not be read (the caller cannot conclude either way).

Jump to

Keyboard shortcuts

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