grpc-go

Does a go-grpc server streaming method not have a context argument?


I am curious to understand why a regular server-side GRPC handler method has a context.Context argument, while a server-side streaming GRPC handler method does not.

Am I supposed to use the server.Context() method to obtain the context? Would that return to me the same kind of context which is passed as an argument to the regular GRPC handlers?

I've looked in the documentation for grpc-go to understand this but did not find useful information.

Thank you!


Solution

  • Yes, the context is passed to the streaming handler via the ServerStream.Context() method (through proto generated code in the "___Server" types e.g. https://github.com/grpc/grpc-go/blob/master/examples/route_guide/routeguide/route_guide_grpc.pb.go#L272).

    All streaming RPCs on the server, regardless of server-, client-, or bidi- pass the context this way. Unary RPCs are the only ones to accept a context.Context as the first parameter.