在飞行中更改序列化的protobuf消息?

huangapple go评论77阅读模式
英文:

Altering serialized protobuf message on the fly?

问题

可以在不必先解组的情况下修改(追加、合并等)序列化的 protobuf 消息吗?我正在使用 golang/protobuf 包

理想情况下,我希望有一个服务可以接收传入的序列化消息,在传递消息到下一个服务之前,动态追加一些字段,就像中间件一样,可以在不必不断解组和组合的情况下向有效载荷添加附加信息。

一些背景信息:系统是实时的,所以我希望尽可能减少开销。

英文:

Is it possible to alter (append, merge, etc) a serialized protobuf message without having to unmarshal it first? I'm using the golang/protobuf package.

Ideally I would like to have a service that can receive incoming serialized messages, append some fields on-the-fly, then pass the message along to the next service -kind of like middleware, where additional information can just be added to the payload without having to constantly unmarshal and marshal.

Some context: the system is realtime, so I'd like to minimize overhead wherever possible.

答案1

得分: 4

根据Protocol Buffers的文档

如你所知,协议缓冲区消息是一系列的键值对。消息的二进制版本只使用字段的编号作为键 - 每个字段的名称和声明类型只能在解码端通过引用消息类型的定义(即.proto文件)来确定。

当消息被编码时,键和值被连接成一个字节流。当消息被解码时,解析器需要能够跳过它不认识的字段。这样,新的字段可以被添加到消息中,而不会破坏不知道这些字段的旧程序。为此,线格式消息中每对的“键”实际上是两个值 - 来自.proto文件的字段编号,加上一个提供足够信息以找到以下值长度的线类型。

因此,要将字段追加到编码的协议缓冲区消息中,只需将编码的字段追加到字节流/切片的末尾即可。

英文:

From the 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.

Therefore, to append a field to an encoded protocol buffer message, you can simply append an encoded field to the end of the byte stream/slice.

huangapple
  • 本文由 发表于 2015年12月19日 05:30:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/34364225.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定