1
votes

I'm using gRPC to communicate between services and protobuf serialization. I haven't really used RPC before, and I'm wondering what the best structure is for the proto files? At the moment, I have all the proto files in one product with the following example layout:

protos/
  identity/
    models/
      Member.proto
    MemberService.proto
  vault/
    models/
      Authentication.proto
      Session.proto
      HttpHeader.proto
    AuthenticationService.proto

I figure that I should separate the models from the actual service definition so I can import a single model without needing the entire service.

Then, each service has the following layout

synatx "proto3";

import "models/Session.proto"

message GetRequest {
  uint64 member_id;
}

message GetResponse {
  Session session;
}

rpc AuthenticationService {
  get (GetRequest) returns (GetResponse);
}

Is there a more canonical way to do this? Should I include the model "message" definitions in the same file as my service? It seems weird to import "../protos-gen/AuthenticationService.grpc.h just to use a single Authentication.proto model.

1

1 Answers

1
votes

Generally, people keep their service definitions next to their message protos. Only when the protos become very large do people break them up, but even this is rare.

The canonical way to structure your protos is to have a single high-level root directory, and reference all the protos, even siblings in the same directory, by their absolute path. The main reason to separate services from message types is if the generated code is getting too large. This is uncommon.