grpcprocetcd

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2026 License: MIT Imports: 14 Imported by: 0 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.

README

grpcproc/etcd

Cluster membership for grpcproc on etcd: nodes register under a lease they keep alive, peers resolve their addresses from it, and when a lease ends (the node stopped, or stopped answering) every node that watches the cluster drops its links to it. A separate module, so grpcproc itself does not depend on the etcd client.

go get github.com/floatdrop/grpcproc/etcd
cluster := grpcprocetcd.New(etcdClient, "/grpcproc/prod") // WithTTL, WithRetry, WithLogger
node, err := grpcproc.NewNode(grpcproc.Config{
    Name:       "orders-1",
    Advertise:  "10.0.0.5:9000", // this node's gRPC server, as peers reach it
    Metadata:   map[string]string{"version": buildVersion}, // in its record, for every node's Members
    Resolver:   cluster,
    Registrar:  cluster,
    Membership: cluster,
    Names:      cluster.Names(), // global names: Global targets and Process.Claim
})
node.Start(ctx) // registers; Stop withdraws

One Cluster value serves every node of a process, and what reaches other nodes through the node: the Inspector forwards through node.Dial with no option of its own, and a leader elector with no Membership of its own follows the node's.

What it does

grpcproc role etcd
Registrar one key per node, <prefix>/nodes/<name>, holding {"name","incarnation","addr","metadata"}, attached to a lease kept alive. If the lease is lost (etcd unreachable longer than the TTL), the node registers again every retry interval until it succeeds, or until it finds a newer incarnation registered. Stop revokes the lease, which removes the key at once.
Resolver reads the key.
Membership lists the keys (every node is reported up), then watches the prefix: a put is a member up, a delete a member down, with the incarnation from the previous value. If the watch breaks (compaction, etcd restarting), it lists again and reports what changed in between.

A node that registers a name already present replaces it: a restarted node supersedes its previous incarnation, whose lease may not have expired yet. Its peers see the new incarnation come up and drop their links to the old one. An older incarnation never replaces a newer one's record, which a compare-and-swap settles: Register fails with ErrSuperseded until the newer one's lease ends, and an instance that was replaced, and comes back to etcd after losing its lease, stops registering if it finds a newer incarnation registered, rather than take the name back. grpcproc peers that have seen the newer one refuse its links too. Incarnations must therefore grow with each start: with the default, the start time, the hosts' clocks must agree.

grpcproc notices a lost link by itself, as fast as gRPC keepalive allows. A node that dies without closing its connections, behind a half-open TCP connection or a network partition, is noticed only when keepalive gives up, or never if keepalive is not configured. Its lease ends after the TTL regardless: every watching node then drops its links, monitors across them fire Down{noconnection}, and pending calls fail with "left the cluster".

Global names

cluster.Names() is the store of the installation's global names: one key per name, <prefix>/names/<name>, holding its holder's PID, under the lease of the holder's node, which this Cluster must have registered.

etcd
A claim a transaction that creates the key, if it does not exist; else a *grpcproc.TakenError naming the holder
A claim that waits (WaitForName) a watch of the key, until it is deleted, then the claim again
A release a delete, if the key's create revision is still the claim's, so that a newer claim is never deleted
The fencing token (Claim.Revision) the key's create revision
Lookup, List one copy per process, listed a page at a time when its first node starts, then watched; a broken watch lists again
Resolve a read of the key

A node's claims are lost once its lease has not been kept alive for three quarters of the TTL: the holders end with name lost before etcd can let the lease end and another node claim their names, as long as the clocks run at the same rate; Claim.Revision fences what they cannot promise. Claims made with KeepOnLoss are made again once the node has registered again, all at once: a key that outlived the old lease moves to the new one with its revision, a free name is claimed with a new one, and a name another process took meanwhile ends the holder with name conflict. A node that dies takes its names with its lease, as its peers drop their links to it.

Documentation

Overview

Package grpcprocetcd keeps a grpcproc cluster's membership in etcd. A node registers under a lease it keeps alive; peers resolve its address from there; and when the lease ends, because the node stopped or because it stopped answering, every node that watches the cluster drops its links to it, firing Down{noconnection} for monitors across them.

cluster := grpcprocetcd.New(etcdClient, "/grpcproc/prod")
node, err := grpcproc.NewNode(grpcproc.Config{
	Name:      "orders-1",
	Advertise: "10.0.0.5:9000",
	Resolver:  cluster, Registrar: cluster, Membership: cluster,
})

Each node is one key, <prefix>/nodes/<name>, holding its name, incarnation, address and metadata as JSON. A node that registers a name already present replaces it: a restarted node supersedes its previous incarnation, whose lease has not yet expired. An older incarnation never replaces a newer one.

Cluster.Names is the store of the installation's global names, under <prefix>/names/, for Config.Names: see Names.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotRegistered is returned by Resolve for a node absent from etcd.
	ErrNotRegistered = errors.New("grpcprocetcd: node is not registered")
	// ErrSuperseded is returned by Register when a newer incarnation of the
	// node is registered.
	ErrSuperseded = errors.New("grpcprocetcd: a newer incarnation is registered")
)
View Source
var ErrNoLease = errors.New("grpcprocetcd: the node holds no lease")

ErrNoLease is the error of a claim for a node this Cluster has not registered, or whose lease is lost: a claim lives under its node's lease.

Functions

This section is empty.

Types

type Cluster

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

Cluster implements grpcproc.Resolver, grpcproc.Registrar and grpcproc.Membership on etcd. One value serves every node of a process.

func New

func New(cli *clientv3.Client, prefix string, opts ...Option) *Cluster

New returns a Cluster keeping its keys under prefix.

func (*Cluster) Members

func (c *Cluster) Members(ctx context.Context) ([]grpcproc.Member, error)

Members lists the registered nodes, ordered by name. Records that cannot be decoded are skipped.

func (*Cluster) Names added in v0.4.0

func (c *Cluster) Names() *Names

Names returns the store of global names that shares this Cluster's etcd and its nodes' leases. Claims are made for nodes this Cluster registered.

func (*Cluster) Register

func (c *Cluster) Register(ctx context.Context, self grpcproc.Member) (func(context.Context) error, error)

Register publishes self under a lease and keeps it alive. If the lease is lost (etcd was unreachable for longer than the TTL), it registers again, every retry interval, until it succeeds or withdraw is called, or until it finds a newer incarnation of self registered: an instance that was replaced does not take the name back. It fails with ErrSuperseded while one is registered, until that one's lease ends; the same incarnation replaces its own record. withdraw revokes the lease, which removes the key at once.

func (*Cluster) Resolve

func (c *Cluster) Resolve(ctx context.Context, node string) (string, error)

Resolve returns the address a node registered.

func (*Cluster) Watch

func (c *Cluster) Watch(ctx context.Context) (<-chan grpcproc.MemberEvent, error)

Watch reports every registered node as up, then follows the prefix: a new or replaced key is a member up, a deleted one (a withdrawn or expired lease) a member down. If the watch breaks (etcd restarted, its history compacted), it lists the members again, every retry interval until that works, and reports what changed in between.

type Names added in v0.4.0

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

Names is the installation's global names in etcd, a grpcproc.Names for Config.Names, from Cluster.Names. Each name is a key, <prefix>/names/<name>, holding its holder's PID, under the lease its node registered with: a node that dies takes its names with it, as its peers drop their links to it.

Every node of the process shares one copy of the names, listed when the first node starts and kept by a watch, which Lookup and List read. A claim is a compare-and-swap that creates the key. A claim that waits watches the key until it is deleted. The node's claims are lost once its lease has not been kept alive for three quarters of the TTL, before etcd can let it end and another node claim the names; claims made with KeepOnLoss are made again once the node has registered again.

func (*Names) Claim added in v0.4.0

func (n *Names) Claim(ctx context.Context, name string, holder grpcproc.PID, opts grpcproc.ClaimOptions, notify func(grpcproc.ClaimEvent)) (grpcproc.NameClaim, error)

Claim creates name for holder under the lease of holder's node, which this Cluster must have registered: ErrNoLease otherwise, or while the lease is lost. A name that exists fails with a *grpcproc.TakenError naming its holder, or, with opts.Wait, is watched until it is deleted, and claimed then, until ctx is done.

func (*Names) List added in v0.4.0

func (n *Names) List(prefix string, limit int) []grpcproc.GlobalName

List returns the names with prefix, ordered by name, at most limit unless limit is 0, as this process's copy has them.

func (*Names) Lookup added in v0.4.0

func (n *Names) Lookup(name string) (grpcproc.PID, bool)

Lookup returns the holder of name, as this process's copy has it.

func (*Names) Resolve added in v0.4.0

func (n *Names) Resolve(ctx context.Context, name string) (grpcproc.PID, bool, error)

Resolve reads the holder of name from etcd.

func (*Names) Watch added in v0.4.0

func (n *Names) Watch(ctx context.Context) error

Watch lists the names, if no node of this process watches them yet, and keeps the copy until the last node's ctx is done.

type Option

type Option func(*Cluster)

Option configures New.

func WithLogger

func WithLogger(l *slog.Logger) Option

WithLogger sets the logger. Default slog.Default().

func WithRetry

func WithRetry(d time.Duration) Option

WithRetry sets the pause before registering again after a lost lease, and before watching again after a broken watch. Default 1s.

func WithTTL

func WithTTL(d time.Duration) Option

WithTTL sets the lease TTL, how long a node that stops answering stays registered. It is rounded up to whole seconds. Default 10s.

Jump to

Keyboard shortcuts

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