Documentation
Nodes and links
How two nodes are linked, what travels between them, and what happens when a peer cannot be reached.
Nodes
A node hosts processes and links to the nodes of other programs. The application constructs it, registers it on its gRPC server, starts it and stops it; grpcproc opens no listener of its own and starts no goroutine outside Start and Stop. One program normally runs one node.
node, err := grpcproc.NewNode(grpcproc.Config{
Name: "warehouse",
Resolver: grpcproc.StaticResolver{"shop": "10.0.0.7:9000"},
DialOptions: []grpc.DialOption{grpc.WithTransportCredentials(creds)},
Admit: grpcproc.AdmitTLS(nil), // a peer's certificate must name its node
})
node.Register(grpcServer) // grpcproc.v1.Node, next to your services
err = node.Start(ctx) // once the server serves
defer node.Stop(ctx) // before the server stopsA node has a Name, which is how peers address it, and an incarnation, a number for this start of it: by default the time it started, in nanoseconds. The two together are its NodeID, and every PID on the node carries both. A node that restarts has the same name and a new incarnation, so a PID from before the restart names a process that no longer exists, and gets noproc rather than a message delivered to a stranger. Addresses has the rest.
Links
Two nodes are linked by two gRPC streams, one in each direction. A node opens its stream to a peer when it first has something to send there: a call to the peer's grpcproc.v1.Node service, held open for as long as both run. The peer opens its own stream back when it first has something to send this way, a reply included. With a stream each way, neither side has to win a race to dial first.
Everything from one node to another travels on that one stream, in order: sends, calls and their replies, monitors and demonitors, the Down a monitor produces, exit requests, and the Hello that opens the stream with the node's identity. A call is an envelope on the same stream as sends, matched to its reply by an id, never a separate RPC, so a call cannot overtake a message sent before it by the same process, or be overtaken by one sent after.
Because the stream is ordered, a process's last message reaches a watcher before the Down that says the process is gone. The writer sends everything queued since its last write as one frame, split at a megabyte or MaxMessageSize, whichever is less, so under load many envelopes share the cost of one gRPC message, and at low load a frame holds one envelope and nothing waits.
The envelope
An envelope is one interaction between two processes. It is a flat message, cheap to decode, and it carries no node names: every envelope on a stream goes from a process of the node that opened it to a process of the node that accepted it, so a PID travels as an incarnation and an id, and the reader fills the node names in from the stream.
message Envelope {
Kind kind = 1; // send, call, reply, monitor, demonitor, down, exit, hello
uint64 from_incarnation = 2; // the process on the sending node; id 0 is the node itself
uint64 from_id = 3;
uint64 to_incarnation = 4; // the process on the receiving node,
uint64 to_id = 5; // unless to_name addresses it by name
string to_name = 6;
uint64 ref = 7; // a call's id, echoed by its reply; a monitor's, by its down
Status status = 8; // a reply's status
string reason = 9; // a reply's error, or the reason of a down or an exit
string body_type = 10; // the body's message type, by full name
bytes body = 11; // and its encoding
Hello hello = 12;
int64 timeout_nanos = 13; // a call's time left, so that clocks need not agree
map<string, string> metadata = 15; // trace context, tenant: never read by grpcproc
bool watch = 16; // a call that asks for a monitor or a link of what answers it
uint64 watched_id = 17; // and, in its reply, the process the watch was placed on
}A body is a protobuf message, sent as its type's full name and its encoding. Generated types register themselves with the protobuf registry, so nothing has to be registered with grpcproc, and the receiving node checks the decoded type against the mailbox's on delivery. The metadata map is carried untouched with every message; it is where a tracer puts its context, as Observability shows.
A peer that is down
The two directions are independent streams, so an envelope the peer sent can still be in flight on the inbound stream when the outbound one fails. A node declares a peer down only once the stream from the peer has ended, when everything the peer sent has been dispatched, in order, or when there was no such stream at all. An outbound failure alone drops that stream, and the next send dials again.
Once a peer is down:
- every monitor that crossed the link fires a
Downwith reasonnoconnection, and every link ends its process with the same reason; - every call waiting on the peer fails with
ErrNoConnection. The peer may have handled it: the request left this node, and only the reply is lost; - calls still queued on the broken stream, never written, fail too, and their messages become dead letters.
Every link failure is a *LinkError, which errors.Is matches to ErrNoConnection. Its Unsent field is what makes a retry safe: it says the message never left this node, because the peer could not be reached, dials to it are backed off, or its link is full, so sending it again cannot deliver it twice. A LinkError without Unsent claims nothing, and the message may have been handled.
A reply or a Down that cannot be routed back to a peer cuts that peer's stream to this node, with a status saying so. The peer waits for those on its own stream, which would stay up while this node cannot reach it back, and it would otherwise never learn that a monitor of its will not fire. Ending its stream makes it see this node as unreachable, as Erlang's single connection would.
Keepalive, timeouts and backoff
A peer that dies without closing its connections, behind a half-open TCP connection or a partition, sends nothing and closes nothing. What turns that silence into a stream error is gRPC keepalive, on both sides: keepalive.ClientParameters in the node's DialOptions, and keepalive.ServerParameters with an enforcement policy on the server. grpcproc does not set it, since the server is the application's; set it, or a silent partition is noticed only by Membership, or never. Configuration has the values the tutorial uses.
A dial is bounded by DialTimeout: resolving the peer, connecting, and the handshake, five seconds by default. After a dial fails, everything routed to that peer fails at once with ErrNoConnection for a while, rather than each send waiting out a dial of its own. The wait starts at a 32nd of DialBackoff and doubles up to it, with jitter. Then one send dials again while the others keep failing, so a hung peer holds one sender at a time: a half-open circuit breaker. Membership reporting the peer up ends the wait, and LinkInfo shows the peer as a down outbound link with its RetryAt and LastError.
Incarnations
Two instances can claim one node name at once: an instance that was replaced but still runs, after a partition heals, or two deploys given the same name. If a stream from either replaced the other's, each would take its peers' links down for the other, and the two would knock each other off for as long as both ran.
So a node remembers, per peer name, the newest incarnation it has seen, over a stream in either direction or from Membership, and refuses an older one: an inbound stream before its Hello, so the old instance's dial fails and backs off, and a dial that reaches one through an address that still points at it. A newer incarnation's streams replace the older one's, whose monitors fire. The node forgets the newest when Membership reports it gone, or on Disconnect, and then lets an older one in.
The rule needs incarnations that grow with each start. The default, the start time, does as long as the hosts' clocks agree to within the time between two starts of a node; a random incarnation would be refused whenever it came out lower than the last. The fence covers grpcproc traffic only: an old instance can still write to a database, which needs fencing of its own.
Stopping a node
Stop asks every process on the node to exit with reason shutdown and waits for them; the ctx bounds the wait. Their Down envelopes are queued on the outbound streams, every stream flushes and half-closes at once, and each is waited on until its peer ends the stream or the ctx ends. Only then are the connections closed, and a Registrar told to withdraw the node. So peers see Down{shutdown}, not noconnection, for the processes a stopping node ran, and a peer that never ends its stream holds its own link to the deadline, not the others.
A call still waiting when its node stops fails with ErrNodeStopped; as with a broken link, the peer may have handled it.
Local and remote sends
A local send appends the message to the mailbox and passes the pointer as it is: no encoding, no copy, and no allocation when the mailbox keeps up. So a message must not be mutated after it is sent. Config.CopyLocal clones every locally delivered message instead.
A remote send encodes the body, queues the envelope on the stream to the peer's node, and returns; a first send to a peer waits for the dial, which is what the ctx of a send bounds. Delivery never waits for a mailbox: one that made it wait in one process would stall the shared stream for every other process behind it. Mailboxes are unbounded by default, and one that WithMailboxLimit bounds refuses instead: a message is a dead letter, a call fails with ErrMailboxFull. Past that, backpressure is the application's, built on the mailbox depth and each link's queue, which are visible. A link's queue is its peer's alone, so it can be bounded without stalling anyone else: with MaxQueued or MaxQueuedBytes set, a send or a call to a peer that cannot keep up fails at once with ErrLinkBusy, as Unsent.