Skip to content

Commit

Permalink
Merge pull request grpc#749 from menghanl/streams_in_serviceinfo
Browse files Browse the repository at this point in the history
Split methods and streams in service info
  • Loading branch information
menghanl authored Jul 11, 2016
2 parents 565b602 + bc88856 commit 13edeef
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 17 deletions.
10 changes: 5 additions & 5 deletions reflection/serverreflection.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,19 +214,19 @@ func (s *serverReflectionServer) serviceMetadataForSymbol(name string) (interfac
return nil, fmt.Errorf("unknown symbol: %v", name)
}

// Search for method in info.
// Search for method name in info.Methods.
var found bool
for _, m := range info.Methods {
if m == name[pos+1:] {
if m.Name == name[pos+1:] {
found = true
break
}
}
if !found {
return nil, fmt.Errorf("unknown symbol: %v", name)
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
31 changes: 24 additions & 7 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,19 @@ func (s *Server) register(sd *ServiceDesc, ss interface{}) {
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 {
// Methods are method names only, without the service name or package name.
Methods []string
Methods []MethodInfo
// Metadata is the metadata specified in ServiceDesc when registering service.
Metadata interface{}
}
Expand All @@ -258,12 +267,20 @@ 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([]MethodInfo, 0, len(srv.md)+len(srv.sd))
for m := range srv.md {
methods = append(methods, m)
methods = append(methods, MethodInfo{
Name: m,
IsClientStream: false,
IsServerStream: false,
})
}
for m := range srv.sd {
methods = append(methods, m)
for m, d := range srv.sd {
methods = append(methods, MethodInfo{
Name: m,
IsClientStream: d.ClientStreams,
IsServerStream: d.ServerStreams,
})
}

ret[n] = &ServiceInfo{
Expand Down
17 changes: 12 additions & 5 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func TestGetServiceInfo(t *testing.T) {
{
StreamName: "EmptyStream",
Handler: nil,
ServerStreams: true,
ServerStreams: false,
ClientStreams: true,
},
},
Expand All @@ -92,10 +92,17 @@ func TestGetServiceInfo(t *testing.T) {
info := server.GetServiceInfo()
want := map[string]*ServiceInfo{
"grpc.testing.EmptyService": &ServiceInfo{
Methods: []string{
"EmptyCall",
"EmptyStream",
},
Methods: []MethodInfo{
MethodInfo{
Name: "EmptyCall",
IsClientStream: false,
IsServerStream: false,
},
MethodInfo{
Name: "EmptyStream",
IsClientStream: true,
IsServerStream: false,
}},
Metadata: []int{0, 2, 1, 3},
},
}
Expand Down

0 comments on commit 13edeef

Please sign in to comment.