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
- Variables
- func ScopeName(scope string) string
- type Config
- type Scope
- func (sc *Scope) Groups() []string
- func (sc *Scope) Join(ctx context.Context, from grpcproc.Caller, group string, pids ...grpcproc.PID) error
- func (sc *Scope) Leave(ctx context.Context, from grpcproc.Caller, group string, pids ...grpcproc.PID) error
- func (sc *Scope) Local(group string) []grpcproc.PID
- func (sc *Scope) Members(group string) []grpcproc.PID
- func (sc *Scope) PID() grpcproc.PID
- func (sc *Scope) Watch[M proto.Message](ctx context.Context, p *grpcproc.Process[M], group string) (Watch, error)
- type Watch
Constants ¶
const ( Label = "groups" RelayLabel = "groups relay" )
Label is the label of a scope's process, by which tools find it; RelayLabel is its relays'.
const DefaultScope = "default"
DefaultScope is the scope of a Config that names none.
Variables ¶
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 ¶
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 ¶
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) 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) Members ¶
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) 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.