From b248593e359eca987585975b06425428295926f2 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Thu, 24 Mar 2022 14:38:23 +0300 Subject: [PATCH] add unknown service handler Signed-off-by: Vasiliy Tolstov --- grpc.go | 14 ++++++++++++-- options.go | 17 +++++++++++------ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/grpc.go b/grpc.go index 2d56fff..c2adf14 100644 --- a/grpc.go +++ b/grpc.go @@ -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 diff --git a/options.go b/options.go index af4b4d6..54079d6 100644 --- a/options.go +++ b/options.go @@ -9,10 +9,11 @@ import ( ) type ( - codecsKey struct{} - grpcOptions struct{} - maxMsgSizeKey struct{} - reflectionKey struct{} + codecsKey struct{} + grpcOptions struct{} + maxMsgSizeKey struct{} + reflectionKey struct{} + unknownServiceHandlerKey struct{} ) // gRPC Codec to be used to encode/decode requests for a given content type @@ -37,8 +38,7 @@ 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. -// +// 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) +}