Merge pull request #749 from menghanl/streams_in_serviceinfo

Split methods and streams in service info
This commit is contained in:
Menghan Li
2016-07-11 16:17:12 -07:00
committed by GitHub
4 changed files with 42 additions and 17 deletions

View File

@ -214,19 +214,19 @@ func (s *serverReflectionServer) serviceMetadataForSymbol(name string) (interfac
return nil, fmt.Errorf("unknown symbol: %v", name) return nil, fmt.Errorf("unknown symbol: %v", name)
} }
// Search for method in info. // Search for method name in info.Methods.
var found bool var found bool
for _, m := range info.Methods { for _, m := range info.Methods {
if m == name[pos+1:] { if m.Name == name[pos+1:] {
found = true found = true
break break
} }
} }
if !found { if found {
return nil, fmt.Errorf("unknown symbol: %v", name) return info.Metadata, nil
} }
return info.Metadata, nil return nil, fmt.Errorf("unknown symbol: %v", name)
} }
// fileDescEncodingContainingSymbol finds the file descriptor containing the given symbol, // fileDescEncodingContainingSymbol finds the file descriptor containing the given symbol,

View File

@ -273,6 +273,7 @@ func testFileContainingSymbol(t *testing.T, stream rpb.ServerReflection_ServerRe
}{ }{
{"grpc.testing.SearchService", fdTestByte}, {"grpc.testing.SearchService", fdTestByte},
{"grpc.testing.SearchService.Search", fdTestByte}, {"grpc.testing.SearchService.Search", fdTestByte},
{"grpc.testing.SearchService.StreamingSearch", fdTestByte},
{"grpc.testing.SearchResponse", fdTestByte}, {"grpc.testing.SearchResponse", fdTestByte},
{"grpc.testing.ToBeExtened", fdProto2Byte}, {"grpc.testing.ToBeExtened", fdProto2Byte},
} { } {

View File

@ -245,10 +245,19 @@ func (s *Server) register(sd *ServiceDesc, ss interface{}) {
s.m[sd.ServiceName] = srv s.m[sd.ServiceName] = srv
} }
// ServiceInfo contains method names and metadata for a service. // MethodInfo contains information about an RPC.
type MethodInfo struct {
// Name is the method name only, without the service name or package name.
Name string
// IsClientStream indicates whether the RPC is a client streaming RPC.
IsClientStream bool
// IsServerStream indicates whether the RPC is a server streaming RPC.
IsServerStream bool
}
// ServiceInfo contains unary RPC method info, streaming RPC methid info and metadata for a service.
type ServiceInfo struct { type ServiceInfo struct {
// Methods are method names only, without the service name or package name. Methods []MethodInfo
Methods []string
// Metadata is the metadata specified in ServiceDesc when registering service. // Metadata is the metadata specified in ServiceDesc when registering service.
Metadata interface{} Metadata interface{}
} }
@ -258,12 +267,20 @@ type ServiceInfo struct {
func (s *Server) GetServiceInfo() map[string]*ServiceInfo { func (s *Server) GetServiceInfo() map[string]*ServiceInfo {
ret := make(map[string]*ServiceInfo) ret := make(map[string]*ServiceInfo)
for n, srv := range s.m { for n, srv := range s.m {
methods := make([]string, 0, len(srv.md)+len(srv.sd)) methods := make([]MethodInfo, 0, len(srv.md)+len(srv.sd))
for m := range srv.md { for m := range srv.md {
methods = append(methods, m) methods = append(methods, MethodInfo{
Name: m,
IsClientStream: false,
IsServerStream: false,
})
} }
for m := range srv.sd { for m, d := range srv.sd {
methods = append(methods, m) methods = append(methods, MethodInfo{
Name: m,
IsClientStream: d.ClientStreams,
IsServerStream: d.ServerStreams,
})
} }
ret[n] = &ServiceInfo{ ret[n] = &ServiceInfo{

View File

@ -79,7 +79,7 @@ func TestGetServiceInfo(t *testing.T) {
{ {
StreamName: "EmptyStream", StreamName: "EmptyStream",
Handler: nil, Handler: nil,
ServerStreams: true, ServerStreams: false,
ClientStreams: true, ClientStreams: true,
}, },
}, },
@ -92,10 +92,17 @@ func TestGetServiceInfo(t *testing.T) {
info := server.GetServiceInfo() info := server.GetServiceInfo()
want := map[string]*ServiceInfo{ want := map[string]*ServiceInfo{
"grpc.testing.EmptyService": &ServiceInfo{ "grpc.testing.EmptyService": &ServiceInfo{
Methods: []string{ Methods: []MethodInfo{
"EmptyCall", MethodInfo{
"EmptyStream", Name: "EmptyCall",
IsClientStream: false,
IsServerStream: false,
}, },
MethodInfo{
Name: "EmptyStream",
IsClientStream: true,
IsServerStream: false,
}},
Metadata: []int{0, 2, 1, 3}, Metadata: []int{0, 2, 1, 3},
}, },
} }