add unknown service handler #103
14
grpc.go
14
grpc.go
@ -145,7 +145,7 @@ func (g *grpcServer) configure(opts ...server.Option) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if opts := g.getGrpcOptions(); opts != nil {
|
if opts := g.getGrpcOptions(); opts != nil {
|
||||||
gopts = append(gopts, opts...)
|
gopts = append(opts, gopts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
g.rsvc = nil
|
g.rsvc = nil
|
||||||
@ -329,12 +329,22 @@ func (g *grpcServer) handler(srv interface{}, stream grpc.ServerStream) (err err
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
if svc == nil {
|
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()
|
return status.New(codes.Unimplemented, fmt.Sprintf("unknown service %s", serviceName)).Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
mtype := svc.method[methodName]
|
mtype := svc.method[methodName]
|
||||||
if mtype == nil {
|
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
|
// process unary
|
||||||
|
17
options.go
17
options.go
@ -9,10 +9,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
codecsKey struct{}
|
codecsKey struct{}
|
||||||
grpcOptions struct{}
|
grpcOptions struct{}
|
||||||
maxMsgSizeKey struct{}
|
maxMsgSizeKey struct{}
|
||||||
reflectionKey struct{}
|
reflectionKey struct{}
|
||||||
|
unknownServiceHandlerKey struct{}
|
||||||
)
|
)
|
||||||
|
|
||||||
// gRPC Codec to be used to encode/decode requests for a given content type
|
// 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
|
// 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 {
|
func MaxMsgSize(s int) server.Option {
|
||||||
return server.SetOption(maxMsgSizeKey{}, s)
|
return server.SetOption(maxMsgSizeKey{}, s)
|
||||||
}
|
}
|
||||||
@ -47,3 +47,8 @@ func MaxMsgSize(s int) server.Option {
|
|||||||
func Reflection(b bool) server.Option {
|
func Reflection(b bool) server.Option {
|
||||||
return server.SetOption(reflectionKey{}, b)
|
return server.SetOption(reflectionKey{}, b)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UnknownServiceHandler enables support for all services
|
||||||
|
func UnknownServiceHandler(h grpc.StreamHandler) server.Option {
|
||||||
|
return server.SetOption(unknownServiceHandlerKey{}, h)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user