偷偷努力,悄无声息地变强,然后惊艳所有人!哈哈,小伙伴们又来学习啦~今天我将给大家介绍《gRPC 是否支持空服务器端消息?》,这篇文章主要会讲到等等知识点,不知道大家对其都有多少了解,下面我们就一起来看一吧!当然,非常希望大家能多多评论,给出合理的建议,我们一起学习,一起进步!
下面的grpc-client代码中,第二个if有必要吗?
status, err := cli.getstatus(ctx, &empty.empty{})
if err != nil {
return err
}
if status == nil {
// this should never happen - right?
return fmt.errorf("nil status result returned")
}
直观上,应该始终检查 go 中的 nil 以防万一。
但是,有一个运行时检查来捕获任何客户端到服务器的 nil 使用情况,例如
status, err := cli.GetStatus(ctx, nil) // <- runtime error
if err != nil {
// "rpc error: code = Internal desc = grpc: error while marshaling: proto: Marshal called with nil"
return err
}
那么是否存在类似的服务器到客户端运行时保证,从而消除对 status == nil 检查的需要?
解决方案
使用人为的服务器示例进一步调查:
func (s *mygrpc) getstatus(context.context, *empty.empty) (*pb.status, error) {
log.println("cli: getstatus()")
//return &pb.status{}, nil
return nil, nil // <- can server return a nil status message (with nil error)
}
并测试客户端/服务器反应:
客户:
error: rpc error: code = internal desc = grpc: error while marshaling: proto: marshal called with nil
服务器:
2019/05/14 16:09:50 cli: getstatus() error: 2019/05/14 16:09:50 grpc: server failed to encode response: rpc error: code = internal desc = grpc: error while marshaling: proto: marshal called with nil
因此,即使有人想要合法返回 nil 值,grpc 传输也不会允许。
注意:服务器端代码仍按预期执行,但就客户端而言,grpc 调用失败。
结论:有效的 (err==nil) 服务器响应总是返回有效的(非 nil)消息。
编辑:
检查 grpc 源可揭示捕获 nil 消息的位置:
func (s *server) sendresponse(t transport.servertransport, stream *transport.stream, msg interface{}, cp compressor, opts *transport.options, comp encoding.compressor) error {
data, err := encode(s.getcodec(stream.contentsubtype()), msg)
if err != nil {
grpclog.errorln("grpc: server failed to encode response: ", err)
return err
}
// ...
}
func encode(c basecodec, msg interface{}) ([]byte, error) {
if msg == nil { // note: typed nils will not be caught by this check
return nil, nil
}
b, err := c.marshal(msg)
if err != nil {
return nil, status.errorf(codes.internal, "grpc: error while marshaling: %v", err.error())
}
// ...
}
这一行的注释是关键:
if msg == nil { // NOTE: typed nils will not be caught by this check }
因此,如果要对我们输入的 nil 使用反射,reflect.valueof(msg).isnil() 将返回 true。以下 c.marshal(msg) 错误 - 并且调用无法向客户端发送消息响应。
是的,这种情况永远不应该发生。 grpc 负责此工作。
本篇关于《gRPC 是否支持空服务器端消息?》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!
本文内容来源于互联网,如有侵权请联系删除。
