Documentation
The actor model
The model behind grpcproc, for a Go developer who has not met it: processes, messages, addresses, and supervision.
grpcproc takes its shape from Erlang, where the unit of a program is a process: state of its own, a queue of messages in front of it, and an address anyone can send to.
Processes
A process is a Go function, run on a goroutine of its own, that takes messages from a mailbox one at a time. Anything can append to the mailbox, from any goroutine or any node; only the process reads from it. Its state lives in local variables nothing else touches, so it needs no lock.
func inventory(p *grpcproc.Process[*shoppb.Reserve]) error {
left := map[string]int64{"apple": 3}
for {
m, err := p.Receive()
if err != nil {
return err // asked to exit, or the node is stopping
}
if left[m.Body.Sku] < m.Body.Qty {
_ = m.Reply(nil, fmt.Errorf("only %d %s left", left[m.Body.Sku], m.Body.Sku))
continue
}
left[m.Body.Sku] -= m.Body.Qty
_ = m.Reply(&shoppb.Reserved{Sku: m.Body.Sku, Left: left[m.Body.Sku]}, nil)
}
}The only way to change the state is to send a message. A caller that wants an answer sends a call and waits; one that does not sends and moves on. Nothing interrupts a process between two messages, so every handler sees a consistent state and leaves one.
A mailbox is typed: this one holds *shoppb.Reserve and nothing else, and the compiler checks every send to it. The type is a protobuf message, so it has a wire encoding already: a process gets the same value from its neighbour in the binary and from a process on another machine.
Messages, not shared memory
Go already says "do not communicate by sharing memory; share memory by communicating". A process is a channel with an owner, an address, and a lifetime you can observe.
A channel has no name: to send on it you need the channel value, so sender and receiver were wired together by whoever created both. A channel does not cross a program boundary, and it cannot tell you that the goroutine reading it has died; a send to it blocks for ever, or never happens, and nothing reports which.
A process's address, a PID or a registered name, can be passed around, stored and sent inside a message, and works from any node in the cluster. And a process can be monitored: when it exits, or when its node is lost, whoever asked is told, with a reason.
Two processes never share a variable; they share only the messages between them. A value that many parts of a program read and write becomes a process that owns it and answers questions about it. That is more typing than a mutex, and it is why the process can move to another machine without anything else changing.
Addresses
A message is sent to an address, never to a process value. The address is a PID, which names a node and a process on it, or a name the process registered on its node. Both are small comparable values that mean the same thing on every node.
Send, Call, Monitor and Exit take an address, look at the node in it, and either deliver within the program or write to the link with that node. The code on both ends is the same either way.
What differs is what can go wrong: a remote call fails when the link breaks, and a monitor on a remote process fires when the node cannot be reached, with a reason that says so. The tutorial runs one application as one program and as four, from the same code: only the configuration changes.
Processes and RPC services
A gRPC service method runs once per request, on whatever goroutine the server gives it, concurrently with every other request. It has no state between calls but what it keeps in a database or behind a mutex. That suits a stateless handler, and grpcproc leaves your services as they are.
A process has state and a queue, and handles one message at a time. A stock that must not be reserved twice, a session that must see its requests in order, a job that runs in steps and can be asked how far it got: each is one process.
In the tutorial an HTTP handler is not a process: it calls one through the node, waits with the request's context, and turns the answer into a status code.
Failure as a message
A process ends when its function returns, and it has a reason: normal for a nil return, the error's text otherwise, panic: … for a panic, or whatever an Exit request asked for. Any process that monitored it receives a Down carrying the reason, after the last message the exiting process sent.
A node that cannot be reached is reported the same way: every monitor on a process there gets a Down with reason noconnection, and every call waiting on it fails. So the code that handles a crash handles a lost machine too.
A goroutine that panics takes the program down, and one that returns leaves no trace. Monitors and links has the rest.
Supervision
Erlang's answer to a process that fails is to let it die and have another one, a supervisor, start it again from a known state. Most failures come from a state the code did not anticipate, and the surest way out of such a state is to leave it. Code that recovers in place keeps the bad state and adds guesses to it.
A supervisor is itself a process. It starts its children, monitors them, and when one exits it applies a policy: restart that child, restart all of them, or give up and exit itself, which its own supervisor then handles. Supervisors of supervisors form a tree.
In grpcproc a supervisor is an ordinary process built on SpawnMonitor, Down and Exit, in the optional grpcproc/actor package. Supervision explains the strategies and what state should live outside the process so a restart can load it.
What grpcproc adds to Go
- Typed mailboxes. A process accepts one protobuf message type, or an interface over several, and its address carries that type. The compiler checks a send on the sending node, and the receiving node checks again on delivery, since types do not cross the wire.
- gRPC as the transport. A node is one gRPC service on a server you own, next to your other services if you have any, with your credentials, interceptors and keepalive. Two nodes hold one stream in each direction, and everything between them travels on it in order.
- Delivery guarantees. At most once, in order per sender, into mailboxes that never block the sender: Processes and messages has the list.
The reasons behind the choices, and what was rejected, are in the design notes.