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

clusterbus

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: GPL-3.0 Imports: 10 Imported by: 0

Documentation

Overview

Package clusterbus is the small surface components use to take part in a cluster: publishing and receiving events on the cluster bus, knowing their own node ID and which nodes are alive, and dropping in-memory caches when another node writes the shared database (Invalidator).

Components take a Bus or Node through a setter or constructor option and treat nil as "standalone": a standalone server never publishes, so its behaviour is exactly that of a single process. internal/app adapts the cluster runtime with FromCluster, and only when the database is shared.

Index

Constants

View Source
const MaxPayload = cluster.MaxPayload

MaxPayload is the largest event payload the bus carries.

View Source
const PublishTimeout = 5 * time.Second

PublishTimeout bounds one Publish made on behalf of a local write.

View Source
const TopicResync = cluster.TopicResync

TopicResync is delivered locally after the bus reconnected: events may have been missed, so every bus-maintained cache must be dropped.

Variables

View Source
var ErrPayloadTooLarge = errors.New("clusterbus: payload too large")

ErrPayloadTooLarge mirrors the cluster bus limit.

Functions

func NewOrigin

func NewOrigin() string

NewOrigin returns a random ID for one component instance, used to recognise (and skip) its own events when the bus echoes them back.

func Tag

func Tag(origin string, payload []byte) []byte

Tag prefixes payload with origin ("origin\npayload").

func Untag

func Untag(msg []byte) (origin string, payload []byte)

Untag splits a Tag-ged payload.

Types

type ApplyFunc

type ApplyFunc func(ctx context.Context, hints []string, all bool)

ApplyFunc reloads or clears a component's caches. all is true after a bus resync, an oversized burst or an event without a hint; otherwise hints lists the keys other nodes reported changing.

type Bus

type Bus interface {
	Publish(ctx context.Context, topic string, payload []byte) error
	Subscribe(topic string, fn func(payload []byte)) (unsubscribe func())
}

Bus publishes and receives cluster events. Publish also delivers to this node's own subscribers (synchronously), so receivers that must ignore their own events tag payloads with an Origin.

type Hub

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

Hub is an in-memory cluster bus shared by several nodes in one process, for tests: Publish delivers synchronously to every subscriber on every node (the publisher's own included, as the PostgreSQL bus does) and enforces MaxPayload. Nodes are live until Kill.

func NewHub

func NewHub() *Hub

NewHub returns an empty hub.

func (*Hub) Kill

func (h *Hub) Kill(id string)

Kill marks a node dead: it drops out of LiveNodes and receives nothing.

func (*Hub) Node

func (h *Hub) Node(id string) Node

Node joins the hub as id.

func (*Hub) Resync

func (h *Hub) Resync(id string)

Resync delivers TopicResync to node id's subscribers, as a bus reconnect would.

type Invalidator

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

Invalidator keeps one component's in-memory caches consistent with writes made by other nodes: after a local write the component calls Notify; every other node's Invalidator for the same component runs apply on a worker goroutine (bursts are coalesced; bus callbacks never block). Its own events are ignored. After a bus resync apply runs with all=true.

A nil *Invalidator is valid and does nothing, which is what a standalone server uses.

func NewInvalidator

func NewInvalidator(bus Bus, component string, log *slog.Logger, apply ApplyFunc) *Invalidator

NewInvalidator subscribes to "invalidate:<component>" on bus. A nil bus returns nil.

func (*Invalidator) Applied

func (iv *Invalidator) Applied() int

Applied reports how many apply runs have completed (tests).

func (*Invalidator) Close

func (iv *Invalidator) Close()

Close unsubscribes and stops the worker after any apply in progress.

func (*Invalidator) Notify

func (iv *Invalidator) Notify(hint string)

Notify tells the other nodes that this node changed the component's data (hint optionally names what changed; "" means everything). It publishes synchronously with a short timeout; failures are logged, not returned: the write itself succeeded, and peers also reload on a bus resync.

type Node

type Node interface {
	Bus
	// NodeID is this node's stable ID.
	NodeID() string
	// LiveNodes is the set of node IDs that heartbeated recently.
	LiveNodes(ctx context.Context) (map[string]bool, error)
}

Node is a Bus plus this node's identity and the cluster's liveness view.

func FromCluster

func FromCluster(c cluster.Cluster) Node

FromCluster adapts the cluster runtime.

Jump to

Keyboard shortcuts

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