Documentation
Contracts from proto
A process’s protocol is a proto service marked grpcproc.v1.process; protoc-gen-grpcproc writes its address, its handler and its topics, as gRPC’s stubs are written.
The code is from examples/guide, whose three contracts are generated this way. Addressing has why an address carries methods; this page has how they come from the proto.
A service is the contract
A process accepts a few message types and answers some of them. Written as a proto service, each rpc says which: a call, answered with its response, or a send, which the process takes and does not answer. One option on the service marks it as a process's rather than a gRPC service's; a service without it is left alone, so both can share a file.
// Runner is the process that runs the tools, registered as "tools" on the
// node that runs the service. protoc-gen-grpcproc writes its address and
// its handler from this.
service Runner {
option (grpcproc.v1.process) = {name: "tools"};
// Run calls a tool, and waits for it no longer than ctx allows. A tool
// that fails is part of the answer, in Failed, and one that panics is
// answered with an error of the panic's text; actor.ErrWorkersBusy, with
// every place taken, and any other error mean no answer.
rpc Run(tools.v1.Run) returns (Ran);
}protoc-gen-grpcproc writes tools_grpcproc.pb.go beside protoc-gen-go's file. For a service with a name it declares:
| Generated | What it is |
|---|---|
RunnerProcess | The name the process is registered under on its node, a constant. |
RunnerAddr | The address: an Addr[proto.Message] with a method per rpc. |
RunnerAddrOf(t Target) | The address of a PID, a Name or a global name. |
RunnerAt(node) | The process on node, by its name. |
RunnerGlobal() | The process that holds the name globally, wherever it runs. |
RunnerHandler | An interface with a method per call and per send, for the process to implement. |
HandleRunnerCall, HandleRunnerMessage | Dispatch a message to the handler, from an actor’s HandleCall and HandleMessage. |
UnimplementedRunnerHandler | A handler that answers ErrType to every call and drops every send, to embed. |
// RunnerAt addresses the process of the Runner service on node, registered as RunnerProcess.
func RunnerAt(node string) RunnerAddr {
return RunnerAddrOf(grpcproc.Name{Node: node, Name: RunnerProcess})
}
// Run calls a tool, and waits for it no longer than ctx allows. A tool
// that fails is part of the answer, in Failed, and one that panics is
// answered with an error of the panic's text; actor.ErrWorkersBusy, with
// every place taken, and any other error mean no answer.
func (c RunnerAddr) Run(ctx context.Context, from grpcproc.Caller, req *Run) (*Ran, error) {
return c.Call[*Ran](ctx, from, req)
}An rpc whose request message has the rpc's name, rpc Run(Run), needs the message's package-qualified name, rpc Run(tools.v1.Run): protobuf resolves the bare name to the rpc itself.
Setting it up
The plugin is a command of the core module, so it is pinned to the core's version as a tool dependency, and buf generate runs it from there: the build is the same on every machine and in CI.
go get -tool github.com/floatdrop/grpcproc/cmd/protoc-gen-grpcproc@latestThe options live in proto/grpcproc/v1/options.proto in the grpcproc repository. buf resolves imports from the Buf Schema Registry, where grpcproc is not published yet, so copy the file into your proto tree, one file importing only descriptor.proto, and keep it out of your own generation with an input that names your paths:
version: v2
inputs:
- directory: proto
paths: [proto/app] # not proto/grpcproc, which is only imported
plugins:
- local: protoc-gen-go
out: .
opt: module=example.com/app
- local: ["go", "tool", "protoc-gen-grpcproc"]
out: .
opt: module=example.com/appbuf's STANDARD lint rules are written for gRPC, and a process's service breaks four of them by design: a Runner is not a RunnerService; a request is the message, not a RunRequest, and a reply is its own message, not a RunResponse; and every send returns Empty, as every topic takes it.
lint:
use:
- STANDARD
except:
- SERVICE_SUFFIX
- RPC_REQUEST_STANDARD_NAME
- RPC_RESPONSE_STANDARD_NAME
- RPC_REQUEST_RESPONSE_UNIQUEbuf compiles your protos against your copy of options.proto, and the plugin knows the options of the grpcproc version it is pinned to. Keep the two at one version: copy the file again when you upgrade, since a release may add fields to it, and a field your copy has but the pinned plugin does not is ignored without a word.
Calls, sends and topics
A call is a plain rpc: the address gets Run(ctx, from, req) (*Ran, error), a Call that names the reply once. A send is an rpc marked option (grpcproc.v1.send) = true, returning google.protobuf.Empty: the address gets Note(ctx, from, req) error, a Send, and NoteAfter(p, d, req) *Timer, a timer to send it later, from a process, since p.SendAfter infers its type from the address and would need the message cast to proto.Message. A request with no fields is left out of every signature: List(ctx, from), SyncAfter(p, d), and List(p, m) on the handler. A field added later changes them, which is when callers should notice.
A topic the process owns is an rpc that takes Empty and returns the event type, marked option (grpcproc.v1.topic). It is not dispatched; it gives the address a typed way to the topic, and the process the name to register it under.
// 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.
service Conversation {
option (grpcproc.v1.process) = {prefix: "conversation/"};
// 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.
rpc Say(conversations.v1.Say) returns (Said);
// Events is the conversation's topic: what the user says, every token of
// its answers, the tools it calls, and the end of each turn.
rpc Events(google.protobuf.Empty) returns (Event) {
option (grpcproc.v1.topic) = {name: "events"};
}
}// ConversationEventsTopic is the name the Events topic of the Conversation service's process for id is registered under on its node.
func ConversationEventsTopic(id string) string { return ConversationProcess(id) + "/events" }
// ConversationEventsAt is the Events topic of the Conversation service's process for id on node, whose events are Event.
//
// Events is the conversation's topic: what the user says, every token of
// its answers, the tools it calls, and the end of each turn.
func ConversationEventsAt(node, id string) pubsub.Topic[*Event] {
return pubsub.Named[*Event](node, ConversationEventsTopic(id))
}The conversation spawns its topic with pubsub.SpawnOwned under ConversationEventsTopic(id), and a follower subscribes to ConversationEventsAt(node, id): the tutorial shows both. A topic of a process with a name is registered under the name as given, RelayStatsTopic; one of a process named per instance under the process's name and the topic's, "conversation/" + id + "/events".
How processes are named
{name: "tools"} is a process registered under one name on each node that runs it. {prefix: "conversation/"} is one named per instance, the prefix and an id: it gets ConversationProcess(id), ConversationAt(node, id) and ConversationGlobal(id) instead. A service with neither, one whose processes the application names itself or reaches by PID, gets only <Service>AddrOf, and may own no topic, since a topic is named after its process.
// ConversationProcess is the name the process of the Conversation service for id is registered under on its node.
func ConversationProcess(id string) string { return "conversation/" + id }
// ConversationAt addresses the process of the Conversation service for id on node, registered as ConversationProcess(id).
func ConversationAt(node, id string) ConversationAddr {
return ConversationAddrOf(grpcproc.Name{Node: node, Name: ConversationProcess(id)})
}What the service cannot say, the package writes by hand around the generated type. The guide's conversation is opened before it is called, so NewConversation(node, id) wraps the address with the node and id and does that first:
// 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
}
// NewConversation is the conversation id on node, the node that runs the
// conversations service: the generated address, which is registered under
// a name made from the id for as long as the process runs, with what opens
// it.
func NewConversation(node, id string) Conversation {
return Conversation{ConversationAddr: ConversationAt(node, id), node: node, id: id}
}A service may not share a name with a message in protobuf. {go_name: "Relay"} lets the proto say RelayProcess while the Go stays RelayAddr.
The process's side
The handler interface has a method per call and per send, with the process, the message and the typed request; a topic gets none, since nothing is sent to it. An actor implements it and hands its HandleCall and HandleMessage to the generated dispatch, which switches on the message's type. A call of a type the service has no rpc for is answered ErrType; a send of one is logged and dropped, as actor.CallsOnly drops a message, since an error from HandleMessage would exit the actor.
// ConversationHandler is what a process of the Conversation service does with what it is
// sent: a method per call, whose result is the reply, as actor.CallHandler's
// is, and one per send. HandleConversationCall and HandleConversationMessage dispatch to it;
// UnimplementedConversationHandler answers for the methods an implementation leaves out.
type ConversationHandler interface {
Say(p *grpcproc.Process[proto.Message], m grpcproc.Msg[proto.Message], req *Say) (*Said, error)
}
// HandleConversationCall answers a call to a process of the Conversation service through s,
// for an actor.CallHandler's HandleCall; a call of another type is answered
// grpcproc.ErrType.
func HandleConversationCall(s ConversationHandler, p *grpcproc.Process[proto.Message], m grpcproc.Msg[proto.Message]) (proto.Message, error) {
switch req := m.Body.(type) {
case *Say:
return s.Say(p, m, req)
}
return nil, grpcproc.ErrType
}// HandleCall hands a call to the Runner service's method for it: Run.
func (r *runner) HandleCall(p *grpcproc.Process[proto.Message], m grpcproc.Msg[proto.Message]) (proto.Message, error) {
return toolsv1.HandleRunnerCall(r, p, m)
}
func (r *runner) Run(p *grpcproc.Process[proto.Message], m grpcproc.Msg[proto.Message], run *toolsv1.Run) (*toolsv1.Ran, error) {A message the process takes from elsewhere, the events of a topic it subscribes to say, is declared as a send too, and dispatched like one. UnimplementedRunnerHandler is for an implementation, or a test's fake, to embed: it answers ErrType to every call and drops every send, so an rpc added to the service breaks nothing that embeds it.
What the plugin refuses
The plugin stops, naming the service or the rpc, rather than write code that would not compile or run:
| Refused | Why |
|---|---|
| A service with both a name and a prefix | A process is named one way. |
| A streaming rpc | A process takes messages and answers calls. |
| A send returning anything but Empty | A send is answered nothing. |
| A topic without a name, or not taking Empty | It is registered under the name, and returns its events. |
| A topic on a service with neither name nor prefix | A topic is named after its process. |
| An rpc that is a send and a topic | One option or the other. |
| Two rpcs of one kind taking the same message | A message is dispatched by its type, to one rpc. |
| An rpc named as a method of Addr or of an actor | Send, Call, Name, PID, Init, HandleCall, HandleDown and the rest would be shadowed or collide. |
| An rpc named like another’s timer, NoteAfter beside the send Note | Two methods of one name. |
| A generated identifier the file declares already | A message named RunnerAddr, or two services generating one name. |
| A topic named like its process, or two processes or topics of one file under one name | The second to register would fail with ErrNameTaken. |
| A go_name that is not an exported identifier | It names the generated API. |
What is on the wire
A body travels under its message's full protobuf name, which the receiving node decodes it by. So a message's name is part of the protocol: renaming Relay to RelayEndpoint breaks the wire between nodes of the two versions even with its fields unchanged, and so does moving it to another package. buf breaking with the FILE category reports a rename as a deleted message, which is the signal to watch for before a rolling upgrade. The rpc names, the service name and the Go names are not on the wire: those can change freely.