GRPC 如何处理多次出现的指针?

问题描述

例如(golang):

type {
  Product struct {
    Name string
  }
  Customer struct {
    Name string
    Products []*Product
  }
}

哪个是正确的行为:

  1. GRPC 遵守 *Product 指针并且只传输一次。
  2. GRPC 会将相同的 *Product 传输到与不同的 Customer 关联的次数

解决方法

迈克尔, 您的消息中不清楚,但我假设您将向 gRPC 服务器发送 Customer 作为请求的一部分。

Golang 会将结构编组到 []byte (https://godoc.org/github.com/golang/protobuf/proto#Marshal) 中,因此消息不会包含指针之类的东西。这将只是一个编码的消息。 (看 https://github.com/golang/protobuf/blob/master/proto/wire.go#L22).

gRPC 不是 Golang 的东西,所以一侧(例如服务器)的指针并不意味着它必须是另一侧(例如客户端)的一个点。

最后,回答您的问题,预期行为是 2但是,您可以更深入地了解 proto buff 序列化 (https://developers.google.com/protocol-buffers/docs/encoding)。我不知道它是如何工作的,但是也许消息被压缩了,所以重复的 []bytes 可能会被丢弃。