replicas

package
v2.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: AGPL-3.0 Imports: 5 Imported by: 1 module, 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 replicas is how code anywhere in Studio (core or Enterprise) cooperates with the other control-plane replicas sharing its database, without being handed the cluster machinery:

  • IsLeader: whether this replica should run singleton background work (aggregations, alerts, syncs, reports, cleanups). One replica holds the leader lease at a time (pkg/cluster.Leadership).
  • OnLeading: run something when this replica becomes the leader, such as start-up work that was skipped because another replica (or this one's dead predecessor) still held the lease.
  • Signal / OnSignal: tell every other replica that some in-memory state derived from the database is stale ("budgets", "governed_metadata", ...). The replica that changed the data refreshes its own state itself; the signal reaches the others through the cluster event log, at least once, within about a second.

Studio runs one instance per process, so this is process-wide. Until pkg/studio connects a backend (and in tests), this process is the only replica: IsLeader is true and signals go nowhere.

Index

Constants

View Source
const (
	// SignalBudgets: budget limits, periods or the team-budget switch
	// changed; every replica clears its budget caches.
	SignalBudgets = "budgets"
	// SignalGovernedMetadata: metadata schemas or vocabularies changed;
	// every replica drops its resolved-schema cache (Enterprise).
	SignalGovernedMetadata = "governed_metadata"
	// SignalSpendRecalculated: a price recalculation rewrote the cost of
	// stored chat records; every replica clears its budget caches and its
	// budget sync re-sums spend (and lets edges lower theirs). The replica
	// that recalculated also delivers it to itself (Deliver), since its
	// budget sync is not the code that changed the data.
	SignalSpendRecalculated = "spend_recalculated"
	// SignalBranding: the logo or favicon was uploaded or reset; replicas
	// drop the copies they hold in memory.
	SignalBranding = "branding"
)

Signal names shared across packages.

Variables

This section is empty.

Functions

func BecameLeader

func BecameLeader()

BecameLeader runs the OnLeading handlers (called by the backend when this replica gains the leader lease).

func Deliver

func Deliver(name string)

Deliver runs the handlers for a signal received from another replica (called by the backend). Code that changes data other parts of its own replica derive state from may also call it, after Signal.

func DeliverAll added in v2.3.0

func DeliverAll()

DeliverAll runs the handlers of every signal: for a replica that may have missed signals (it could not read the cluster event log for longer than events are kept), so that everything a signal would have refreshed is refreshed.

func IsLeader

func IsLeader() bool

IsLeader reports whether this replica runs singleton background work.

func OnLeading

func OnLeading(fn func()) (remove func())

OnLeading registers fn to run each time this replica becomes the leader. Leader-only work that checks IsLeader on a timer uses it to catch up at once instead of waiting a whole interval: a replica restarted after a crash may only get the lease a moment after its start-up run was skipped. Register before checking IsLeader, so no change falls between the two. fn runs on the lease's goroutine: it must be quick (wake a loop). The returned function removes it.

func OnSignal

func OnSignal(name string, fn func()) (remove func())

OnSignal registers fn to run when another replica signals name. fn runs on the cluster log's reader goroutine: it must be quick (clear a cache, trigger a background reload). The returned function removes it.

func SetBackend

func SetBackend(b Backend)

SetBackend connects the cluster; nil disconnects it.

func Signal

func Signal(ctx context.Context, name string)

Signal tells the other replicas that the state called name is stale. It does not call this replica's own handlers: the caller has refreshed its state already. Failures are logged; the other replicas then refresh on their own schedule (TTL) if they have one, or serve stale state.

Types

type Backend

type Backend interface {
	IsLeader() bool
	// Signal publishes name to the other replicas.
	Signal(ctx context.Context, name string) error
}

Backend is the cluster behind the package (pkg/studio provides it).

Jump to

Keyboard shortcuts

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