9

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.

azizbekian
  • 60,783
  • 13
  • 169
  • 249
gorros
  • 1,411
  • 1
  • 18
  • 29

1 Answers1

5

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

user803422
  • 2,636
  • 2
  • 18
  • 36