Batching technique using Protobufs
Asked Answered
B

1

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.

Broaddus answered 4/10, 2019 at 11:56 Comment(0)
B
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

Bicorn answered 11/10, 2019 at 14:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.