Documentation
Quick start
Install grpcproc, put a node on your gRPC server, spawn a process, and call it from another node by name.
This page builds examples/quickstart piece by piece; the code is cut from it.
Install
go get github.com/floatdrop/grpcprocgrpcproc needs Go 1.27 or later. Messages are protobuf messages, and a mailbox holds one type. The example uses two, compiled with protoc-gen-go:
// Reserve asks for qty items of sku. The answer is Reserved.
message Reserve {
string sku = 1;
int64 qty = 2;
}
// Reserved is what is left of sku after a reservation.
message Reserved {
string sku = 1;
int64 left = 2;
}A node on your gRPC server
A node is grpcproc in one program: it hosts processes and links to the nodes of other programs. Construct it, register it on your gRPC server, start it once the server serves, and stop it before the server stops:
// peers is where each node's gRPC server listens: a static address book
// here, grpcproc/etcd in a real cluster.
var peers = grpcproc.StaticResolver{}
// node runs a node on its own gRPC server, which a service may share with
// its other APIs.
func node(ctx context.Context, name string) *grpcproc.Node {
lis, err := net.Listen("tcp", "127.0.0.1:0")
check(err)
peers[name] = lis.Addr().String()
n, err := grpcproc.NewNode(grpcproc.Config{
Admit: grpcproc.AdmitAll, // loopback; AdmitTLS with mTLS across a network
Name: name,
Resolver: peers,
DialOptions: []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())},
})
check(err)
srv := grpc.NewServer()
n.Register(srv) // grpcproc.v1.Node, next to the service's own
go func() { _ = srv.Serve(lis) }()
check(n.Start(ctx))
return n
}Config needs a Name, which is how peers address the node, a Resolver, which turns a peer's name into an address to dial, and an Admit, which decides which peers may link. The example's resolver is a map; Discovery covers the rest. DialOptions are for every connection the node opens to a peer: the credentials, and the keepalive that turns a silent partition into a broken link. Configuration goes through every field.
Register mounts one gRPC service, grpcproc.v1.Node, next to yours. Peers open a stream to it and this node opens one to them; every message, call, reply and monitor between the two travels on those streams, in order.
A process
A process is a function over a typed mailbox, run on its own goroutine until it returns. This one holds a stock of apples. Its mailbox holds *shoppb.Reserve and nothing else, and every message it takes is a call: the sender waits for an answer, which Reply gives, as a message or an error.
// inventory is a process: a goroutine with a mailbox. The mailbox holds
// *shoppb.Reserve and nothing else, and every message is a call, answered
// with a *shoppb.Reserved or an error.
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)
}
}Receive blocks until a message arrives, and returns an error when the process should stop: it was asked to exit, or its node is stopping. The process returns that error. A process that returns nil ends with reason normal; one that returns an error, or panics, ends with that as its reason, and whoever monitors it learns which.
Spawn and call
Spawn runs the function as a process on the node; WithName registers it under a name for as long as it runs. From another node, the process is a node name and a process name, and Named makes an address from the two:
// Two nodes, each on its own gRPC server; see node() below.
warehouse, shop := node(ctx, "warehouse"), node(ctx, "shop")
// The process runs on warehouse, under a name.
_, err := warehouse.Spawn(inventory, grpcproc.WithName("stock"))
check(err)
// 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")
for range 2 {
r, err := stock.Call[*shoppb.Reserved](ctx, shop, &shoppb.Reserve{Sku: "apple", Qty: 2})
if err != nil {
fmt.Println("reserve failed:", err) // the handler's error, as a *grpcproc.RemoteError
continue
}
fmt.Println("reserved, left:", r.Left)
}The address is typed: Addr[*shoppb.Reserve] says what the process behind it accepts, so the compiler checks every send to it, wherever it runs. Call sends and waits for the reply, typed by what the caller asks for. An error the handler returned comes back as a *RemoteError, its text carried across the link. The context bounds the whole call. The same call works with a local address.
Monitor
Monitor asks for a Down when the target exits, or when its node cannot be reached, and the Down arrives in the watcher's mailbox like any message, after everything the target sent it. Here a process on the shop monitors the stock and then asks it to exit:
// A monitor across nodes works as one within a node does. A process on
// shop monitors stock, then asks it to exit; the Down arrives with the
// reason, as it would for a crash or a lost node.
exited := make(chan string)
_, err = shop.Spawn(func(p *grpcproc.Process[proto.Message]) error {
p.Monitor(stock)
check(p.Exit(stock, "closing"))
m, err := p.Receive() // the Down: nothing else is sent to this process
if err != nil {
return err
}
exited <- m.Down.Reason
return nil
})
check(err)
fmt.Println("stock exited:", <-exited)
_, err = stock.Call[*shoppb.Reserved](ctx, shop, &shoppb.Reserve{Sku: "apple", Qty: 1})
fmt.Println("no such process:", errors.Is(err, grpcproc.ErrNoProc))The watcher's mailbox is proto.Message, the untyped one, since it expects nothing but the Down. Exit asks a process anywhere to stop, with a reason; the process's next Receive returns an error, it returns, and its watchers get the reason. The last call finds no process under the name, and fails with ErrNoProc.
Run
git clone https://github.com/floatdrop/grpcproc && cd grpcproc/examples
go run ./quickstartreserved, left: 1
reserve failed: only 1 apple left
stock exited: closing
no such process: trueNext steps
- The actor model: why processes and mailboxes. Processes and messages: what a process can do.
- Actors: a struct with a method per kind of message, in place of a receive loop. Supervision: restarting it when it fails.
- The tutorial: an AI agent runtime, with a process per conversation, run as one program or as a program per node from the same code.