0
votes

I'm creating client side streaming method which proto file definition should be looking similar to this:

service DataService {
    rpc Send(stream SendRequest) returns (SendResponse) {}
}
 
message SendRequest {
    string id = 1;
    bytes data = 2;
}
 
message SendResponse {
}

The problem here is that ID is sent with each streaming message even it is needed only once. What's your recommendation and most optimal way for such a use cases? One hacky approach would be to set ID only once within first message and after that left if blank. But this API is supposed to be used by third party and method definition like above doesn't explaining that well. I don't think something like this is supported either:

service DataService {
    rpc Send(InitialSendRequest, stream DataOnlyRequest) returns (SendResponse) {}
}

I'm currently considering SendRequest message to be something like this, but will have to check how optimal is this compared to the first case considering proto marshaling:

message SendRequest {
  oneof request{
    string id = 1;
    bytes data = 2;
  }
}
1

1 Answers

1
votes

Your approach of using a oneof with the fields clearly documented saying id is expected only in the first message on the stream and that server implementations will terminate the stream if id is set on subsequent messages on the stream sounds good to me.

The following is a usage of the above described pattern in grpc-lb-v1. Even though the grpc team is moving away from grpc-lb-v1, the above mentioned pattern is a commonly used one.

I'm not very sure about its implications with respect to proto marshaling. That might be a question for the protobuf team.

Hope this helps.