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 ¶
- Variables
- type Cluster
- func (c *Cluster) Members(ctx context.Context) ([]grpcproc.Member, error)
- func (c *Cluster) Names() *Names
- func (c *Cluster) Register(ctx context.Context, self grpcproc.Member) (func(context.Context) error, error)
- func (c *Cluster) Resolve(ctx context.Context, node string) (string, error)
- func (c *Cluster) Watch(ctx context.Context) (<-chan grpcproc.MemberEvent, error)
- type Names
- func (n *Names) Claim(ctx context.Context, name string, holder grpcproc.PID, ...) (grpcproc.NameClaim, error)
- func (n *Names) List(prefix string, limit int) []grpcproc.GlobalName
- func (n *Names) Lookup(name string) (grpcproc.PID, bool)
- func (n *Names) Resolve(ctx context.Context, name string) (grpcproc.PID, bool, error)
- func (n *Names) Watch(ctx context.Context) error
- type Option
Constants ¶
This section is empty.
Variables ¶
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") )
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 (*Cluster) Members ¶
Members lists the registered nodes, ordered by name. Records that cannot be decoded are skipped.
func (*Cluster) Names ¶ added in v0.4.0
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) Watch ¶
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
Lookup returns the holder of name, as this process's copy has it.