Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batching technique using Protobufs

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.

like image 947
gorros Avatar asked Oct 04 '19 11:10

gorros


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

like image 143
user803422 Avatar answered Oct 13 '22 19:10

user803422