I'm trying to detect the gRPC server shutdown in Go gRPC client code like
import (
pb "mysample.com/api/rpc"
"google.golang.org/grpc"
)
var stream pb.SearchProductService_MarketStreamClient
for {
r, err := stream.Recv()
if err == io.EOF {
log.Println("Info: server close")
break
} else if err != nil {
log.Println("Err: grpc code:", grpc.Code(err))
if err != nil {
log.Fatal(err)
}
}
outs := printOrderbook(r)
fmt.Println(outs)
if outf != nil {
outf.WriteString(time.Now().Format("030405.000 ") +
outs + "
")
}
}
The output message is
mysample.go:122: Err: grpc code: Unavailable
panic: rpc error: code = Unavailable desc = transport is closing
I'm trying to search 'transport is closing' in Go's source and $GOPATH/src/, but I can't not found such phrase. Is there any smart method to handle such deeper error?
gRPC always need to return errors properly, if you do not handle errors the connection will go into TRANSIENT_FAILURE state and you cannot request any query with this client connection. Catch the error properly and return it using grpc error codes. You can find deeper insites here https://www.grpc.io/docs/guides/error/ and use this inbuilt package grpc: google.golang.org/grpc/status
conn *grpc.ClientConn conn.GetState() will give the current connection state