Documentation
Addresses
How a message finds a process: PIDs, names on a node, global names, and the typed address over them.
A message goes to an address, never to a process value. An address is small, comparable, and means the same on every node, so it can be stored and sent inside a message. There are two kinds, a PID and a name, and a typed wrapper over both.
PID
A PID identifies one process, anywhere in the cluster, for the whole of its life:
type PID struct {
Node string // the node it runs on
Incarnation uint64 // that node's start
ID uint64 // the process, on that start
}It prints as <shop.1718.4>. Spawn returns its address, p.PID() is the process's own, and Msg.From is the sender's. A PID is a value: it can be compared, used as a map key, and put inside a protobuf message to tell another process whom to answer.
The incarnation makes a PID safe to keep. A node that restarts numbers its processes from one again, so without it the PID of a process from before the restart would point at whichever process now has that number. With it, the PID names a process that no longer exists: a send to it is a dead letter, a call fails with ErrNoProc, and a monitor on it fires Down{noproc}. By default the incarnation is the node's start time; Nodes says what else it fences.
Name
A process may register a name on its node when it is spawned, and hold it until it exits. A Name is the node and that name; it prints as {stock@warehouse}.
_, err := warehouse.Spawn(inventory, grpcproc.WithName("stock"))
// ErrNameTaken while another process holds the name.
pid, ok := warehouse.Whereis("stock") // on this node, nowNames are per node: two nodes can each have a stock, told apart by the node name. A name is freed when its process exits and taken by whatever is spawned under it next, so an address by name outlives any one process: a supervisor restarts a child under the same name and its callers notice only the gap. A message sent by name while nobody holds it is a dead letter, and a monitor placed by name fires at once with noproc.
A local name is found by knowing which node it is on, from configuration or from a message that carried its PID. A name of the whole installation is a global name, as Erlang's global is.
Global names
A global name belongs to the installation, not to a node: whichever process holds it, wherever it runs, is what Global{"room:42"} reaches. It is for a process that moves, restarted on another node or placed where there is room, and for many of them: one per room, per tenant, per order. A process claims the name itself, and holds it until it exits or releases it:
func room(p *grpcproc.Process[*roomspb.Command]) error {
claim, err := p.Claim(p.Context(), "room:42", grpcproc.KeepOnLoss())
if err != nil {
return err // a *grpcproc.TakenError names the holder; errors.Is(err, grpcproc.ErrTaken)
}
_ = claim.Revision() // the fencing token for what it writes elsewhere
// … serve the room; the name goes when this function returns
}
// From any node: the node looks the name up and sends to the holder's PID.
r, err := grpcproc.AddrOf[*roomspb.Command](grpcproc.Global{Name: "room:42"}).
Call[*roomspb.Joined](ctx, node, join)The names live in a store, Config.Names: etcd in production, an in-memory one in every grpcproctest cluster. Each node keeps a copy, so a send never waits on the store; a message sent a moment after the holder moved can reach the old one, and fail as a process that does not exist would. For a name no one holds, a call fails with ErrNoProc, a monitor gets Down{noproc} with the global name in Down.Name, a message is a dead letter. Where the copy's lag matters, before starting what another node may have started, node.Names().Resolve asks the store itself.
| Option | What it changes |
|---|---|
| none | If the store loses the claim, the node cut off from etcd past its lease, the holder ends with name lost before another node can claim the name: never two holders. For what must be unique, a ledger. |
KeepOnLoss() | The holder runs on, Held() false, and holds the name again once the store is back. If another process claimed it meanwhile, the old holder ends with name conflict. For what must stay up, a room whose calls go on while etcd is away. |
WaitForName() | A held name does not fail the claim: it waits, until its ctx is done, for the name to be free, and claims it then. A standby: the same process on every node, one active. |
In a test, c.CutNames("b") cuts node b off from the store as a partition from etcd would, and c.RestoreNames("b") lets it back. ProcessInfo.Globals, and the Inspector, show what a process holds. The design notes have the rest: what the store does, the release on exit, the cost at a hundred thousand names.
Typed addresses and targets
An Addr[M] is a PID or a Name together with the message type the process behind it accepts. It is what every typed send and call takes, and it comes from three places:
Spawn | Returns the new process's address, typed by the function it was given. p.Addr() is the process's own. |
Named[M](node, name) | An address by name, with the type the caller asserts. One service reaches another this way: the name and the type are the contract, exported by the package that owns the process. |
AddrOf[M](target) | Types an untyped target: a PID that came in a message, say. Nothing checks the assertion until a message is delivered. |
// From shop it is a node name and a process name. The address carries the
// mailbox type, so the compiler checks what is sent to it.
stock := grpcproc.Named[*shoppb.Reserve]("warehouse", "stock")A Target is anything a message can go to: a PID, a Name, or any Addr. Monitor, Link and Exit take a target, since they do not care about the mailbox's type; SendTo and CallTo take one too, and the type is checked on delivery only.
Address types with methods
A typed send or call goes through the address, which takes the sender as an argument, a Caller: the *Node, or a *Process from inside a handler. Every call site names its reply, stock.Call[*shoppb.Reserved](ctx, node, req). The package that owns the process can name it once, by declaring the protocol as a proto service marked grpcproc.v1.process; protoc-gen-grpcproc then writes an address type with a method per rpc, a Call that names the reply, or a Send for an rpc marked grpcproc.v1.send:
// ConversationAddr addresses a process of the Conversation service; its methods are its rpcs.
//
// Conversation is one conversation's process, registered as "conversation/"
// and its id: what it accepts, and the topic it publishes on.
// protoc-gen-grpcproc writes its address from this; address.go adds what
// every caller does first, opening it.
type ConversationAddr struct{ grpcproc.Addr[proto.Message] }
// ConversationAddrOf addresses t, a PID, a Name or a global name, as a process of the Conversation service.
func ConversationAddrOf(t grpcproc.Target) ConversationAddr {
return ConversationAddr{grpcproc.AddrOf[proto.Message](t)}
}
// Say tells the conversation what the user said, and returns once its
// turn has begun: after the turns before it. The answer is published to
// whoever follows the conversation.
func (c ConversationAddr) Say(ctx context.Context, from grpcproc.Caller, req *Say) (*Said, error) {
return c.Call[*Said](ctx, from, req)
}A caller then writes c.Say(ctx, p, req) from a handler, or c.Say(ctx, node, req) from anywhere else, and names no reply. The package adds, by hand, what every caller must do first, around the generated type: the guide's conversation starts its process if none runs.
// Conversation is one conversation as a caller reaches it: its process, by
// the name it is registered under, and the supervisor that starts it. The
// generated ConversationAddr is its protocol; Say and Follow add what every
// caller does first, starting the conversation's process if none runs.
type Conversation struct {
ConversationAddr
node, id string
}
// Say tells the conversation what the user said, and returns once its turn
// has begun: after the turns before it. The answer is published to whoever
// follows the conversation. A process that was idle may end between its
// opening and the call: it is then opened again, once.
func (c Conversation) Say(ctx context.Context, from grpcproc.Caller, text string) (said *Said, err error) {
for range 2 {
if err = c.open(ctx, from); err != nil {
return nil, err
}
if said, err = c.ConversationAddr.Say(ctx, from, &Say{Text: text}); !errors.Is(err, grpcproc.ErrNoProc) {
break
}
}
return said, err
}The sender is an argument rather than part of the address because who sends matters: a process's call carries the metadata of the message it is handling, and an actor keeps its addresses in fields but has its process only inside a handler. The type embeds Addr, so it is still a Target with its raw Call and Send. The same service gives the process its side: a ConversationHandler interface with a method per rpc, and HandleConversationCall, which dispatches a call to it from an actor's HandleCall and answers ErrType for any other message. Contracts from proto has every form the plugin writes; the tutorial's contracts are written this way.
Type checks
The type on an address is the sender's claim. On the sending node the compiler enforces it: a Send to an Addr[*shoppb.Reserve] takes a *shoppb.Reserve and nothing else. The claim does not travel: on the wire a message is its type's full name and its encoding, and the receiving node decodes it and checks, on delivery, that the process's mailbox accepts it.
A remote sender that names the right process with the wrong type does not reach it. The message never enters the mailbox: it is a dead letter with reason type, and a caller gets ErrType. The same check runs for a local send. An address that Spawn returned always passes it; Named or AddrOf with the wrong type, or an untyped SendTo, is a dead letter on the same node too.
Dead letters
A message that cannot be delivered is a dead letter: there is no process at the PID, nobody holds the name, the type is wrong, or the mailbox is full. For a Send that is not an error: the node counts the dead letter and reports it to Hooks.OnDeadLetter and to subscribers of its events. Errors and exit reasons lists the reasons.
A Call waits, so it fails: with ErrNoProc when there is no such process, or when the process exits before answering, and with ErrType when the process does not take the request or the reply is not what the caller asked for. A send returns an error only for what its own node could not do: the message could not be encoded or was too large, the peer could not be reached or its link is full, or the context ended while waiting for a first connection to it.
Observability shows where dead letters are counted and watched; a steady trickle of them under one name is usually a caller with a stale configuration.
Metadata
Every message carries a Metadata, a map[string]string that grpcproc passes along and never reads: trace context, a tenant, a request id, whatever the application's interceptors would carry. From outside a process it is set on the context:
ctx = grpcproc.WithMetadata(ctx, grpcproc.Metadata{"tenant": "acme"})
r, err := stock.Call[*shoppb.Reserved](ctx, node, reserve)
md := grpcproc.MetadataFrom(ctx) // reads it backInside a process it needs no context. A process remembers the metadata of the message it is handling, and every Send, Call and SendAfter it makes meanwhile inherits it. A request that enters at the edge with a trace id leaves the same id on every message its handling causes, across processes and nodes. A call's context can add to what is inherited, and m.Context(parent) puts a message's metadata into a context for code that wants one, a database client say; for a call it also ends at the caller's deadline, as Send and Call describes.
grpcproc/otel builds its traces on this: a span opened when a message is taken, and every message sent while handling it a child of that span.
The node's own PID
A message sent from outside any process still has a sender: the node's pseudo-process, node.PID(), the node's name and incarnation with ID zero. It has no mailbox, so a process cannot reply to it with a send; a call from it is answered through the call, as any other. It shows up in Msg.From, in dead letters and in traces.