Skip to content

Commit

Permalink
Split methods and streams in service info
Browse files Browse the repository at this point in the history
  • Loading branch information
menghanl committed Jul 11, 2016
1 parent 565b602 commit a518fa9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
19 changes: 15 additions & 4 deletions reflection/serverreflection.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,19 +214,30 @@ func (s *serverReflectionServer) serviceMetadataForSymbol(name string) (interfac
return nil, fmt.Errorf("unknown symbol: %v", name)
}

// Search for method in info.
// Search for method in info.Methods.
var found bool
for _, m := range info.Methods {
if m == name[pos+1:] {
found = true
break
}
}
if !found {
return nil, fmt.Errorf("unknown symbol: %v", name)
if found {
return info.Metadata, nil
}

// Search for stream in info.Streams.
for _, m := range info.Streams {
if m == name[pos+1:] {
found = true
break
}
}
if found {
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,
Expand Down
1 change: 1 addition & 0 deletions reflection/serverreflection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ func testFileContainingSymbol(t *testing.T, stream rpb.ServerReflection_ServerRe
}{
{"grpc.testing.SearchService", fdTestByte},
{"grpc.testing.SearchService.Search", fdTestByte},
{"grpc.testing.SearchService.StreamingSearch", fdTestByte},
{"grpc.testing.SearchResponse", fdTestByte},
{"grpc.testing.ToBeExtened", fdProto2Byte},
} {
Expand Down
14 changes: 9 additions & 5 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,12 @@ func (s *Server) register(sd *ServiceDesc, ss interface{}) {
s.m[sd.ServiceName] = srv
}

// ServiceInfo contains method names and metadata for a service.
// ServiceInfo contains unary rpc names, streaming rpc names and metadata for a service.
type ServiceInfo struct {
// Methods are method names only, without the service name or package name.
// Methods are unary rpc names only, without the service name or package name.
Methods []string
// Streams are streaming rpc names only, without the service name or package name.
Streams []string
// Metadata is the metadata specified in ServiceDesc when registering service.
Metadata interface{}
}
Expand All @@ -258,16 +260,18 @@ type ServiceInfo struct {
func (s *Server) GetServiceInfo() map[string]*ServiceInfo {
ret := make(map[string]*ServiceInfo)
for n, srv := range s.m {
methods := make([]string, 0, len(srv.md)+len(srv.sd))
methods := make([]string, 0, len(srv.md))
for m := range srv.md {
methods = append(methods, m)
}
for m := range srv.sd {
methods = append(methods, m)
streams := make([]string, 0, len(srv.sd))
for s := range srv.sd {
streams = append(streams, s)
}

ret[n] = &ServiceInfo{
Methods: methods,
Streams: streams,
Metadata: srv.mdata,
}
}
Expand Down
2 changes: 2 additions & 0 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ func TestGetServiceInfo(t *testing.T) {
"grpc.testing.EmptyService": &ServiceInfo{
Methods: []string{
"EmptyCall",
},
Streams: []string{
"EmptyStream",
},
Metadata: []int{0, 2, 1, 3},
Expand Down

0 comments on commit a518fa9

Please sign in to comment.