Just started to work on gRPC project in golang and wonder how the context used in gRPC
Server
for example in scenario of streaming client on server side I have a function that implement the call:
rpc SendLogs(stream LogMessage) returns(google.protobuf.Empty) {}
func (s serverImpl) SendLogs(stream pb.xxxxServer) error {
}
First: what is this error
that should be returned ? This is internal app (server) logic error ? Is this a status of .Recv()
operation ?
Sometimes stream.Context().Err()
already has an error of Deadline on Cancelled just before stream.Recv()
– what does it mean ? Is this error propagated from client ? Should I return this error ?
Other RPC functions implementations (that does not stream – like unary call) received context
as a first parameter – what is this context
? how it should be used ? Is it only to pass metadata between server and client ?
Client side
To create streaming client on client I first create gRPC server like this:
conn, err = grpc.NewClient(address, opts...)
...
...
client := pb.NewXXXXXXXClient(conn)
...
streamingClient, err = client.SendLogs(context.Background(), grpc.EmptyCallOption{})
what is this context parameter in in function ?
Could it be cancelled ?
In case canceling on this context make sense – what happens when it cancelled ? gRPC will close the stream and disconnect ? Receive GOAWAY response ?
Should I close conn.Close()
and re-create gRPC client
? I understand that I can not continue and use context
that was already cancelled ?
When I call streamingClient.CloseSend()
function does it close the stream ? What will be received (?) on server side ?
Does gRPC will try to send multiple times begore giving up or I should take care of it ? Could I continue to send if error
returned from .Send()
function or the network stream is already unusable at this moment ?
How I could configure the gRPC itself to re-connect / re-try in case of network failures (such as time-out or ‘connection reset by peer’ error)