Batching technique using Protobufs

Viewed 624

Is there an efficient technique to batch different Protobuf events while sending over HTTP?

The goal is to have a list of multi-type Protobuf messages in one request. One idea I have is to separate messages in small arrays and specify their type to be able to deserialize them on the server.

1 Answers

You can use some Any message type combined with repeated, as follows:

message Any {
    string type_url = 1;
    bytes value = 2;
}

message Envelope {
    repeated Any events = 1;
}

Then, in your code:

  • when serializing, you must set type_url according to the message type that you serialize in value
  • when deserializing, you must read type_url to know which type is contained in value, and deserialize accordingly

The example above reproduces the google/protobuf/any, that is documented here: https://developers.google.com/protocol-buffers/docs/proto3#any

Related