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 ¶
- type Bus
- type Elector
- type Event
- type Kind
- type Local
- type Memberlist
- func (b *Memberlist) Close() error
- func (b *Memberlist) Enabled() bool
- func (b *Memberlist) GetBroadcasts(int, int) [][]byte
- func (b *Memberlist) LocalState(bool) []byte
- func (b *Memberlist) Members() int
- func (b *Memberlist) MergeRemoteState([]byte, bool)
- func (b *Memberlist) NodeMeta(int) []byte
- func (b *Memberlist) NotifyJoin(n *memberlist.Node)
- func (b *Memberlist) NotifyLeave(n *memberlist.Node)
- func (b *Memberlist) NotifyMsg(msg []byte)
- func (b *Memberlist) NotifyUpdate(*memberlist.Node)
- func (b *Memberlist) OtherInstances() (instances, users int, err error)
- func (b *Memberlist) Publish(e Event)
- func (b *Memberlist) SendTo(node string, e Event)
- func (b *Memberlist) SetLocalUsers(fn func() int)
- func (b *Memberlist) Subscribe(fn func(Event))
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) Start ¶
func (e *Elector) Start()
Start tries to become leader right away and then keeps checking.
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) SetLocalUsers ¶
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))