grpcprocetcd

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2026 License: MIT Imports: 13 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
    Resolver:   cluster,
    Registrar:  cluster,
    Membership: cluster,
})
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"}, 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".

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 and address 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.

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")
)

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) 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 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