We have a gRPC server written in golang. For one of the RPCs, we request data from the Google Maps gRPC API. Once we receive a response from the Google Maps RPC, we do some calculations and return a response to the enclosing RPC (defined by our server).
Naturally, we receive a Context object when the RPC is called on our server.
My question is: Should we pass this same Context object to the Google Maps RPC? Or, should we instead create a new Context object (using context.Background()
), and then pass this to the Google Maps API?
The context you received contains the cancellation and timeout, so it would be good to use the same context to make the new RPC. Otherwise you need to manually pass these signals.
A side on the metadata, metadata in the received context will not be automatically forwarded to the new RPC, see:
As far as i have figured, 1 context is valid for the life cycle of 1 and only 1 rpc. So as soon as we realize that we are about to make a whole different rpc call, we should make a new context. However, The metadata of the new context may be inspired by the metadata of the current context. An RPC here may be considered analogous to a single activity/service/broadcast in android.