cluster

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: Apache-2.0 Imports: 16 Imported by: ? 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 cluster passes live updates between Etherpad instances that share one database.

The database stays the only source of truth: what is sent here are hints ("pad X is at revision N", the receiver loads the revisions itself) and messages that exist only in memory (chat broadcasts, who is in a pad). A lost hint delays an update until the next one or the periodic catch-up, it never loses or corrupts data.

Instances find each other through the database (table cluster_nodes, like Keycloak's jdbc-ping) and talk directly with hashicorp/memberlist, which handles membership and failure detection.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bus

type Bus interface {
	// Publish sends e to every other instance.
	Publish(e Event)
	// SendTo sends e to one instance (an Event's Origin).
	SendTo(node string, e Event)
	// Subscribe registers fn for events from other instances. Events may
	// arrive out of order and, rarely, not at all: revision hints only make
	// the receiver look at the database, which is always right.
	Subscribe(fn func(Event))
	// Enabled reports whether there may be other instances.
	Enabled() bool
	// Members is the number of instances, this one included.
	Members() int
	// SetLocalUsers tells the bus how to count this instance's connected
	// users, which it shares with the others.
	SetLocalUsers(fn func() int)
	// OtherInstances returns the other live instances and their connected
	// users, as last reported (a few seconds old at most).
	OtherInstances() (instances, users int, err error)
	Close() error
}

Bus connects the instances.

type Elector

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

Elector keeps one instance among those sharing a database as the leader for a background job. It is safe to use from several goroutines.

func NewElector

func NewElector(store db.LeaderMethods, name string, logger *zap.SugaredLogger) *Elector

NewElector returns an elector for the job name; call Start to begin.

func (*Elector) IsLeader

func (e *Elector) IsLeader() bool

IsLeader reports whether this instance currently runs the job.

func (*Elector) Start

func (e *Elector) Start()

Start tries to become leader right away and then keeps checking.

func (*Elector) Stop

func (e *Elector) Stop()

Stop gives up leadership so another instance can take over at once.

func (*Elector) WithInterval

func (e *Elector) WithInterval(d time.Duration) *Elector

WithInterval sets how often the elector checks; for tests.

type Event

type Event struct {
	Kind    Kind            `json:"k"`
	Pad     string          `json:"p,omitempty"`
	Rev     int             `json:"r,omitempty"`
	Session string          `json:"s,omitempty"`
	Author  string          `json:"a,omitempty"`
	Payload json.RawMessage `json:"m,omitempty"`
	// Origin is the sending instance; set by the bus.
	Origin string `json:"o,omitempty"`
}

Event is one message between instances.

type Kind

type Kind string

Kind is the type of an Event.

const (
	// KindRevision: pad Pad has new revisions (up to Rev).
	KindRevision Kind = "rev"
	// KindRoom: send Payload to the pad's clients, except session Session.
	KindRoom Kind = "room"
	// KindJoin: session Session joined pad Pad; Payload announces it to the
	// pad's clients. Receivers answer with their users (KindSession).
	KindJoin Kind = "join"
	// KindSession: send Payload to session Session.
	KindSession Kind = "session"
	// KindAuthor: send Payload to the sessions of author Author in Pad.
	KindAuthor Kind = "author"
	// KindDeleted: pad Pad was deleted.
	KindDeleted Kind = "deleted"
	// KindAll: send Payload to every client.
	KindAll Kind = "all"
	// KindErased: author Author was erased; close its sessions.
	KindErased Kind = "erased"
	// KindAccounts: accounts, roles or assignments changed; forget the
	// cached rights (lib/accounts).
	KindAccounts Kind = "accounts"
	// KindLogout: a pad login session ended (Session: its key); its
	// connections are closed (lib/ws).
	KindLogout Kind = "logout"
)

type Local

type Local struct{}

Local is the bus of a single instance: nothing to send.

func (Local) Close

func (Local) Close() error

func (Local) Enabled

func (Local) Enabled() bool

func (Local) Members

func (Local) Members() int

func (Local) OtherInstances

func (Local) OtherInstances() (int, int, error)

func (Local) Publish

func (Local) Publish(Event)

func (Local) SendTo

func (Local) SendTo(string, Event)

func (Local) SetLocalUsers

func (Local) SetLocalUsers(func() int)

func (Local) Subscribe

func (Local) Subscribe(func(Event))

type Memberlist

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

Memberlist is the bus between instances on one database.

func NewMemberlist

func NewMemberlist(cfg settings.Cluster, store db.DataStore, logger *zap.SugaredLogger) (*Memberlist, error)

NewMemberlist starts the gossip listener, registers this instance in the database and joins the other registered instances.

func (*Memberlist) Close

func (b *Memberlist) Close() error

Close leaves the cluster and removes this instance from the registry.

func (*Memberlist) Enabled

func (b *Memberlist) Enabled() bool

func (*Memberlist) GetBroadcasts

func (b *Memberlist) GetBroadcasts(int, int) [][]byte

func (*Memberlist) LocalState

func (b *Memberlist) LocalState(bool) []byte

func (*Memberlist) Members

func (b *Memberlist) Members() int

func (*Memberlist) MergeRemoteState

func (b *Memberlist) MergeRemoteState([]byte, bool)

func (*Memberlist) NodeMeta

func (b *Memberlist) NodeMeta(int) []byte

func (*Memberlist) NotifyJoin

func (b *Memberlist) NotifyJoin(n *memberlist.Node)

func (*Memberlist) NotifyLeave

func (b *Memberlist) NotifyLeave(n *memberlist.Node)

func (*Memberlist) NotifyMsg

func (b *Memberlist) NotifyMsg(msg []byte)

func (*Memberlist) NotifyUpdate

func (b *Memberlist) NotifyUpdate(*memberlist.Node)

func (*Memberlist) OtherInstances

func (b *Memberlist) OtherInstances() (instances, users int, err error)

OtherInstances reads the registry: the instances besides this one with a heartbeat in the last three intervals, and their connected users at that heartbeat. An instance stopped normally removes itself; one that was killed drops out after those three intervals.

func (*Memberlist) Publish

func (b *Memberlist) Publish(e Event)

func (*Memberlist) SendTo

func (b *Memberlist) SendTo(node string, e Event)

func (*Memberlist) SetLocalUsers

func (b *Memberlist) SetLocalUsers(fn func() int)

SetLocalUsers sets how this instance counts its connected users; the count goes out with every heartbeat.

func (*Memberlist) Subscribe

func (b *Memberlist) Subscribe(fn func(Event))

Jump to

Keyboard shortcuts

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