The problem

In the process of using GRPC, it is found that there are two ways to send matedata: grpc.SendHeader(CTX, MD) and grPC. SetTrailer(CTX, MD).

Why send metadata via SetTrailer when you can send metadata via SendHeader? What is the difference and connection between the two?

process

First of all, we need to know that GRPC communication modes fall into four categories

  1. Unary RPC mode
  2. Server flow RPC mode
  3. Client stream RPC mode
  4. Two-way flow RPC mode

Then, let’s look at the difference between unary RPC mode and bidirectional streaming RPC mode

In unitary RPC mode, the client message is composed of head-content 1-EOS, and the server message is composed of head-content 1-trailerCopy the code

In two-way flow RPC mode, client messages are composed of header - content 3- content 1- content 2-EOS through multiple multiple collection mode, and server messages are composed of header - content 1- content 2- content 3- TrailerCopy the code

conclusion

The receiving timing of header and trailer is different. It can be seen that in unary mode, header and trailer arrive at the client together, and the client obtains metadata from header or trailer in the same way. However, in flow mode, header arrives first. After receiving multiple streams, trailer is finally acquired. The timing of acquisition is different, so GRPC provides two ways for us to send metadata

This problem is found in the chapter of sending metadata in gRPC and cloud native application development, and found a more satisfactory answer through Google, record the process, and hope to solve the same question you!

The resources

GRPC and cloud native application development