add unknown service handler

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2022-03-24 14:38:23 +03:00
parent dda5d46e47
commit b248593e35
2 changed files with 23 additions and 8 deletions

14
grpc.go
View File

@ -145,7 +145,7 @@ func (g *grpcServer) configure(opts ...server.Option) error {
}
if opts := g.getGrpcOptions(); opts != nil {
gopts = append(gopts, opts...)
gopts = append(opts, gopts...)
}
g.rsvc = nil
@ -329,12 +329,22 @@ func (g *grpcServer) handler(srv interface{}, stream grpc.ServerStream) (err err
*/
if svc == nil {
if g.opts.Context != nil {
if h, ok := g.opts.Context.Value(unknownServiceHandlerKey{}).(grpc.StreamHandler); ok {
return h(srv, stream)
}
}
return status.New(codes.Unimplemented, fmt.Sprintf("unknown service %s", serviceName)).Err()
}
mtype := svc.method[methodName]
if mtype == nil {
return status.New(codes.Unimplemented, fmt.Sprintf("unknown service %s.%s", serviceName, methodName)).Err()
if g.opts.Context != nil {
if h, ok := g.opts.Context.Value(unknownServiceHandlerKey{}).(grpc.StreamHandler); ok {
return h(srv, stream)
}
}
return status.New(codes.Unimplemented, fmt.Sprintf("unknown service method %s.%s", serviceName, methodName)).Err()
}
// process unary

View File

@ -13,6 +13,7 @@ type (
grpcOptions struct{}
maxMsgSizeKey struct{}
reflectionKey struct{}
unknownServiceHandlerKey struct{}
)
// gRPC Codec to be used to encode/decode requests for a given content type
@ -38,7 +39,6 @@ func Options(opts ...grpc.ServerOption) server.Option {
//
// MaxMsgSize set the maximum message in bytes the server can receive and
// send. Default maximum message size is 4 MB.
//
func MaxMsgSize(s int) server.Option {
return server.SetOption(maxMsgSizeKey{}, s)
}
@ -47,3 +47,8 @@ func MaxMsgSize(s int) server.Option {
func Reflection(b bool) server.Option {
return server.SetOption(reflectionKey{}, b)
}
// UnknownServiceHandler enables support for all services
func UnknownServiceHandler(h grpc.StreamHandler) server.Option {
return server.SetOption(unknownServiceHandlerKey{}, h)
}