go - 即时更改序列化的 protobuf 消息?

标签 go protocol-buffers

是否可以更改(追加、合并等)序列化的 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/

相关文章:

go - 如何覆盖 go 模块中的依赖项?

protocol-buffers - gcloud 端点部署错误未解决类型

c++ - 使用 MSYS2/Mingw32 编译 gRPC

pointers - 在 Go 中,您将如何访问传递给期望空接口(interface)的函数的底层数组?

c# - 使用 Protocol buffers/Protobuf 的 PInvoke 通用字符串格式

protocol-buffers - Protobuf3 : How to describe map of repeated string?

java - 如果添加一个 boolean 字段,PB 消息的序列化大小会增加 2 个字节吗?

json - for 循环上的 goroutine channel

Vim-go - 使用制表符而不是空格缩进

macos - Golang http 服务器从一个请求中获得了两个远程端口