是否可以更改(追加、合并等)序列化的 protobuf
消息而无需先对其进行解码?我正在使用 golang/protobuf package .
理想情况下,我希望有一个服务可以接收传入的序列化消息,即时附加一些字段,然后将消息传递给下一个服务——有点像中间件,可以在其中添加额外的信息有效载荷,而不必不断地解码和编码。
一些上下文:系统是实时的,所以我想尽可能地减少开销。
最佳答案
来自documentation on protocol buffers :
As you know, a protocol buffer message is a series of key-value pairs. The binary version of a message just uses the field's number as the key – the name and declared type for each field can only be determined on the decoding end by referencing the message type's definition (i.e. the .proto file).
When a message is encoded, the keys and values are concatenated into a byte stream. When the message is being decoded, the parser needs to be able to skip fields that it doesn't recognize. This way, new fields can be added to a message without breaking old programs that do not know about them. To this end, the "key" for each pair in a wire-format message is actually two values – the field number from your .proto file, plus a wire type that provides just enough information to find the length of the following value.
因此,要将字段附加到编码的 Protocol Buffer 消息,您可以简单地将编码字段附加到字节流/slice 的末尾。
关于go - 即时更改序列化的 protobuf 消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34364225/