groups

package
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2026 License: MIT Imports: 18 Imported by: 1 module, 1 package

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.

README

grpcproc/groups

Process groups for grpcproc, after Erlang's pg: named sets of processes, on any node, that any process can list and watch. A process joins a group on its own node; every node that runs the same scope learns of it, and of its leaving, its exit, or its node going away. A package of grpcproc's, built on its public API alone.

go get github.com/floatdrop/grpcproc # groups is a package of the core module
// every node that takes part runs the scope
workers, err := groups.Start(node, groups.Config{Scope: "jobs"})

// a process joins on its own node
err = workers.Join(ctx, p, "resize", p.PID())

// any node lists the members, from its own copy, without a message
members := workers.Members("resize") // every node's, ordered by node

// a process that must hear of changes watches the group
w, err := workers.Watch(ctx, p, "resize")
for {
    m, err := p.Receive()
    // a *groupsv1.Changed from w.From; the Down with w.Ref is the end of them
}

The guide walks through examples/groups: workers on two nodes in a group, found and called from a third, which watches a worker exit and a node stop.

Scopes

A scope is a process on each node that takes part, registered as groups/<scope> and labelled groups. Nodes whose scopes share a name share their groups; Config.Scope is default when empty. A node finds the other nodes of its scope through:

Config The nodes it asks
Peers these, from the start, whatever Membership reports
Membership each node it reports up, and forgets each it reports down, unless Peers names it; unset, the node's own Config.Membership
Select of those Membership reports up, only the ones it takes: by their Metadata, say, to keep a scope to the nodes that run it. One it stops taking is only answered, as a node that asks this one is

A node that asks this one is answered whatever these say, and so is one that writes to it with no relay to it running: it is asked again, unless it waits to be, and syncs again. With neither Peers nor a Membership, a scope knows only the nodes that ask it.

Start fails when the node already runs a scope of that name. A scope that ends, asked to exit or with its node, takes its node's memberships with it: the other nodes drop them, and its copy is empty.

Joining and leaving

Call What it does
scope.Join(ctx, from, group, pids…) adds processes of the scope's node to group; once each, however often joined
scope.Leave(ctx, from, group, pids…) takes them out of it; a process that is not in it is left alone

from is the Node or a Process, as for Addr.Call. A process stays in a group until it leaves it or exits. Both refuse a process of another node (ErrNotLocal), and a group's name that is empty, not valid UTF-8, or longer than 1 KiB (ErrGroup); Join refuses a process that does not run (grpcproc.ErrNoProc). A refusal changes nothing.

Reading and watching

scope.Members(group), scope.Local(group) and scope.Groups() read the node's copy under a lock, from any goroutine, without a message: every node's members, this node's own, and the groups with members. The copy is eventually consistent. This node's joins are in it when Join returns; another node's, once that node's message arrives.

scope.Watch(ctx, p, group) has the scope send p, a process of the scope's node, a *groupsv1.Changed for each change to the group: who joined and who left. Watch.Members are the members when the watch began, and the changes follow from there. p's mailbox must accept *groupsv1.Changed (grpcproc.ErrType otherwise). Each watch is ended by its own w.Cancel(p), or by p's exit; a process with several watches of one group gets each change once. The Down with w.Ref says the scope ended.

Between nodes

A scope reaches each other node's scope through a relay, a process of its own labelled groups relay, so that a send or a monitor that waits for a dial holds up the relay and not the scope; a call from another node is answered from a process of its own, for the same reason. The relay says hello to the other scope, watches the one that answers, and carries this node's members to it: the whole list once (a Sync), then each change (an Update), both in parts of about 32 KiB. A relay that fails to send ends, and the next one syncs again.

What happens to another node Its members on this node
its scope answers, and syncs are its list, then its changes
it runs no such scope, or its scope ends dropped: it is absent
it cannot be reached dropped: it is unreachable
Membership reports it gone dropped, and the node forgotten, unless Peers names it; news of an incarnation older than one the node is known to have changes nothing

An absent or unreachable node is asked again after Config.Retry (1s), doubling with each loss in a row up to 32 times as long; a relay that lasts longer than the wait before it starts the count over. A Hello from the node, or Membership reporting a newer incarnation of it, ends the wait; one that came while the relay to it was still asking ends the wait that relay's loss starts, and has an older scope the relay finds replaced. The wait is the node's, so one forgotten and made again, as the same incarnation, waits it out. A node that is only answered is forgotten instead when its scope ends, or after five tries in vain, and asked again when it writes once its wait is over: one that writes while it waits waits. A node whose scope answers the Hello with an error is absent, not unreachable.

Inspection

A scope's WithInspect map counts its groups, members, watchers and peers by state. It answers a groupsv1.Query through grpcproc.WithQuery, which a read-only Inspector serves: its groups whose names start with a prefix, with each one's members on every node and on this one, or one group's members, and the other nodes it knows with their states. grpcprocctl groups, the MCP tools process_groups and group_members, and the web UI's Groups page read it. See docs/DESIGN.md for why it works as it does.

Documentation

Overview

Package groups is process groups on grpcproc, after Erlang's pg: named sets of processes, on any node, that any process can list and watch. A process joins a group on its own node; every node that runs the same scope learns of it, and of its leaving, or its exit, or its node going away. It uses only grpcproc's public API, so it can be ignored or replaced.

Every node that takes part runs a scope:

workers, err := groups.Start(node, groups.Config{Scope: "jobs"})

A process joins, and any process on any node of the scope finds the members, read from the node's own copy without a message:

err = workers.Join(ctx, p, "resize", p.PID())
…
members := workers.Members("resize") // every node's, ordered

A process that must hear of changes watches a group:

w, err := workers.Watch(ctx, p, "resize")
for {
	m, err := p.Receive()
	…	// a *groupsv1.Changed from w.From; a Down with w.Ref, the end of them
}

The copies are eventually consistent: a node's own joins are in its copy once Join returns, another node's as soon as its message arrives. A node that cannot be reached has its members dropped until it can again.

Index

Constants

View Source
const (
	Label      = "groups"
	RelayLabel = "groups relay"
)

Label is the label of a scope's process, by which tools find it; RelayLabel is its relays'.

View Source
const DefaultScope = "default"

DefaultScope is the scope of a Config that names none.

Variables

View Source
var (
	// ErrNotLocal is what Join, Leave and Watch answer for a process of
	// another node than the scope's.
	ErrNotLocal = errors.New("groups: not a process of the scope's node")
	// ErrGroup is what they answer for a group's name that is empty, not
	// valid UTF-8, or longer than 1 KiB.
	ErrGroup = errors.New("groups: a group's name must be valid UTF-8, of 1 to 1024 bytes")
)

Functions

func ScopeName

func ScopeName(scope string) string

ScopeName is the name a scope's process is registered under, on every node that runs the scope.

Types

type Config

type Config struct {
	// Scope names the groups: the nodes whose scopes share a name share
	// their groups. Default DefaultScope.
	Scope string
	// Peers are nodes asked for their members from the start, whatever
	// Membership reports.
	Peers []string
	// Membership reports the nodes of the cluster: each one it reports up
	// that Select takes is asked for its members, and each one it reports
	// down is forgotten, unless Peers names it. Unset, it is the node's own
	// Config.Membership. With neither, only Peers are asked, and the nodes
	// that ask this one.
	Membership grpcproc.Membership
	// Select, if set, picks which nodes Membership reports up are asked:
	// those that run the scope, by their Metadata, say; one it stops taking
	// is only answered, as a node that asks this one is.
	// It runs on the scope's membership watch, not on its process.
	Select func(grpcproc.Member) bool
	// Retry is how long after a node could not be reached, or ran no such
	// scope, it is asked again, doubling with each loss in a row up to 32
	// times as long, and how long after the membership watch ends it is
	// started again. Default 1s.
	Retry time.Duration
}

Config configures a node's scope.

type Scope

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

Scope is a node's part of a scope: its process, and its copy of every node's groups.

func Start

func Start(n *grpcproc.Node, cfg Config) (*Scope, error)

Start runs n's part of the scope cfg names: a process registered as ScopeName(cfg.Scope), which lasts until it is asked to exit or n stops. The groups of a scope that ended are empty, and its processes left them.

func (*Scope) Groups

func (sc *Scope) Groups() []string

Groups returns the groups with members, on any node, ordered.

func (*Scope) Join

func (sc *Scope) Join(ctx context.Context, from grpcproc.Caller, group string, pids ...grpcproc.PID) error

Join adds pids, processes of the scope's node, to group, from the Node or a Process, as for grpcproc.Addr.Call. A process joins a group once, and is in it until it leaves it or exits. When Join returns, Members on this node has them; the other nodes hear of it after.

func (*Scope) Leave

func (sc *Scope) Leave(ctx context.Context, from grpcproc.Caller, group string, pids ...grpcproc.PID) error

Leave removes pids, processes of the scope's node, from group. A process that is not in it is left alone.

func (*Scope) Local

func (sc *Scope) Local(group string) []grpcproc.PID

Local returns the members of group on the scope's node, ordered.

func (*Scope) Members

func (sc *Scope) Members(group string) []grpcproc.PID

Members returns the members of group on every node of the scope, as this node knows them, ordered by node, incarnation and id.

func (*Scope) PID

func (sc *Scope) PID() grpcproc.PID

PID is the scope's process.

func (*Scope) Watch

func (sc *Scope) Watch[M proto.Message](ctx context.Context, p *grpcproc.Process[M], group string) (Watch, error)

Watch has the scope send p a *groupsv1.Changed for each change to group's members, until p cancels the watch or exits. p must be a process of the scope's node, and its mailbox must accept *groupsv1.Changed. Each Watch is ended by its own Cancel; a process that holds several of one group gets each change once.

type Watch

type Watch struct {
	Group string
	// From is the scope the changes come from, as *groupsv1.Changed.
	From grpcproc.PID
	// Ref is the watcher's monitor of From. The Down with Ref means no more
	// changes come.
	Ref grpcproc.Ref
	// Members are the group's members when the watch began; the changes
	// that follow are to them.
	Members []grpcproc.PID
	// contains filtered or unexported fields
}

Watch is a process's watch of a group.

func (Watch) Cancel

func (w Watch) Cancel[M proto.Message](p *grpcproc.Process[M])

Cancel ends the watch: p stops monitoring the scope and tells it to send no more. Changes already in p's mailbox stay there.

Directories

Path Synopsis
proto

Jump to

Keyboard shortcuts

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