首頁  >  文章  >  後端開發  >  在編組包含它的訊息時,我可以重複使用現有的 protobuf 二進位嗎? (protobuf3)

在編組包含它的訊息時,我可以重複使用現有的 protobuf 二進位嗎? (protobuf3)

WBOY
WBOY轉載
2024-02-06 10:30:11439瀏覽

在编组包含它的消息时,我可以重用现有的 protobuf 二进制文件吗?(protobuf3)

問題內容

protobuf 定義如下:

syntax = "proto3"

message hugemessage {
    // omitted
}

message request {
    string name = 1;
    hugemessage payload = 2;
}

在一種情況下,我收到了某人發來的 hugemessage,我想用其他欄位打包它,然後將該訊息傳輸給其他人。因此,我必須將hugemessage 二進位檔案unmarshal放入go結構中,將其打包到request中,然後再次marshal。由於 hugemessage 的 hgue 大小,unmarshalmarshal 的成本難以承受。那麼我可以重用 hugemessage 二進位檔案而不更改 protobuf 定義嗎?

func main() {
    // receive it from file or network, not important.
    bins, _ := os.ReadFile("hugeMessage.dump")
    var message HugeMessage
    _ = proto.Unmarshal(bins, &message) // slow
    request := Request{
        name: "xxxx",
        payload: message,
    }
    requestBinary, _ := proto.Marshal(&request) // slow
    // send it.
    os.WriteFile("request.dump", requestBinary, 0644)
}


正確答案


#簡短的回答是:不,沒有簡單或標準的方法來實現這一點。

最明顯的策略是按照您目前的方式進行操作 - 解組 hugemessage,將其設為 request,然後再次編組。 golang protobuf api 表面並沒有真正提供一種方法來做更多事情 - 這是有充分理由的。

也就是說,有方法可以實現您想要做的事情。但這些不一定安全或可靠,因此您必須權衡該成本與您現在擁有的成本。

避免解組的一種方法是利用訊息通常序列化的方式;

message request {
    string name = 1;
    hugemessage payload = 2;
}

..相當於

message request {
    string name = 1;
    bytes payload = 2;
}

.. 其中 payload 包含針對某些 hugemessage 呼叫 marshal(...) 的結果。

所以,如果我們有以下定義:

syntax = "proto3";

message hugemessage {
  bytes field1 = 1;
  string field2 = 2;
  int64 field3 = 3;
}

message request {
  string name = 1;
  hugemessage payload = 2;
}

message rawrequest {
  string name = 1;
  bytes payload = 2;
}

以下程式碼:

req1, err := proto.Marshal(&pb.Request{
    Name: "name",
    Payload: &pb.HugeMessage{
        Field1: []byte{1, 2, 3},
        Field2: "test",
        Field3: 948414,
    },
})
if err != nil {
    panic(err)
}

huge, err := proto.Marshal(&pb.HugeMessage{
    Field1: []byte{1, 2, 3},
    Field2: "test",
    Field3: 948414,
})
if err != nil {
    panic(err)
}

req2, err := proto.Marshal(&pb.RawRequest{
    Name:    "name",
    Payload: huge,
})
if err != nil {
    panic(err)
}

fmt.Printf("equal? %t\n", bytes.Equal(req1, req2))

輸出 equal? true

這種「怪癖」是否完全可靠尚不清楚,也不能保證它會無限期地繼續發揮作用。顯然 rawrequest 類型必須完全鏡像 request 類型,這並不理想。

另一種選擇是以更手動的方式建立訊息,即使用 protowire 套件 - 再次,隨意,建議謹慎。

以上是在編組包含它的訊息時,我可以重複使用現有的 protobuf 二進位嗎? (protobuf3)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:stackoverflow.com。如有侵權,請聯絡admin@php.cn刪除