Search code examples
grpcgrpc-go

How GRPC handle pointer that appear more then once?


For example (golang):

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

Which is the correct behavior:

  1. GRPC honor the *Product pointer and transfer it only once.
  2. GRPC will transfer the same *Product as many times as it associated to different Customer.

Solution

  • Michael, It is not clear on your message, but I am assuming that you will send a Customer as part of your request to a gRPC server.

    Golang will marshal the struct into []byte (https://godoc.org/github.com/golang/protobuf/proto#Marshal), so the message will not have such thing as a pointer. It will be just an encoded message. (see https://github.com/golang/protobuf/blob/master/proto/wire.go#L22).

    gRPC is not a Golang thing, so a pointer on a side (e.g. server) does not mean it must be a point on the other side (e.g. client).

    Finally, answering your question, the expected behavior is 2. However, you may take a deeper look into proto buff serialization (https://developers.google.com/protocol-buffers/docs/encoding). I have no idea how it works, but maybe the message is compressed, so repeated []bytes maybe be discarded.