0
votes

I need to get some data using gRPC from a server which have different types but are semantically related. You can think of it as a data which can have types A, B, and C. I was thinking about what is the proper method of transferring this data to client. I personally can think of three different methods:

  • Using single message with oneof:

In this method, I just define a single message as the following:

message MyData {
  oneof test_oneof {
    DataA a = 1;
    DataB b = 2;
    DataC c = 3;
  }
}

Now I just add a single rpc method which can get different types of data. The problem with this method is extensibility. I will not be able to update my message by adding new message types DataD to this oneof as stated here (or at least that is what I understood, though I think oneof will be somehow useless with this limitation).

  • Using single message with Any:

In this method, I just define a single message as the following:

import "google/protobuf/any.proto";

message MyData {
  google.protobuf.Any data = 1;
}

Here, I need a single rpc method. This method is also extensible, but the drawback is that Any types are still under development. In addition, code will be more complex because of required reflection codes.

  • Using multiple rpc calls:

In this method, I just define a single rpc call for each data type. This method is extensible too, but it creates a huge amount of similar code and I personally don't like it at all. I personally think since these data are semantically related, they need to be transferred using single rpc call.

What do you think as the best method for transferring this data using gRPC and protobuf?

1

1 Answers

0
votes

It's hard to tell which one is the best because they have their own pros and cons. In general, however, having multiple rpc calls in this case is recommended because it's clear to clients as to how to use and easy to extend them later. You still can combine some of them into a rpc with optional parameters, though. Once you have multiple RPCs for each message, you might be able to make a common handler not to repeat the code.