From a518fa911dd84d882644a3d7c578c1f5b94fa9dc Mon Sep 17 00:00:00 2001 From: Menghan Li Date: Fri, 1 Jul 2016 11:09:27 -0700 Subject: [PATCH 1/3] Split methods and streams in service info --- reflection/serverreflection.go | 19 +++++++++++++++---- reflection/serverreflection_test.go | 1 + server.go | 14 +++++++++----- server_test.go | 2 ++ 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/reflection/serverreflection.go b/reflection/serverreflection.go index b044a3a8..c3e63574 100644 --- a/reflection/serverreflection.go +++ b/reflection/serverreflection.go @@ -214,7 +214,7 @@ 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:] { @@ -222,11 +222,22 @@ func (s *serverReflectionServer) serviceMetadataForSymbol(name string) (interfac break } } - if !found { - return nil, fmt.Errorf("unknown symbol: %v", name) + if found { + return info.Metadata, nil } - 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 nil, fmt.Errorf("unknown symbol: %v", name) } // fileDescEncodingContainingSymbol finds the file descriptor containing the given symbol, diff --git a/reflection/serverreflection_test.go b/reflection/serverreflection_test.go index aeb31e14..ca9610e2 100644 --- a/reflection/serverreflection_test.go +++ b/reflection/serverreflection_test.go @@ -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}, } { diff --git a/server.go b/server.go index 7dabfc64..413c019c 100644 --- a/server.go +++ b/server.go @@ -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{} } @@ -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, } } diff --git a/server_test.go b/server_test.go index 7c1e54dd..d7bf2f98 100644 --- a/server_test.go +++ b/server_test.go @@ -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}, From 0ea9f970c0ccc94fcbc3346cc9f0689ff81b00a9 Mon Sep 17 00:00:00 2001 From: Menghan Li Date: Wed, 6 Jul 2016 11:47:40 -0700 Subject: [PATCH 2/3] Add StreamInfo for streaming types --- reflection/serverreflection.go | 2 +- server.go | 28 +++++++++++++++++++++------- server_test.go | 10 ++++++---- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/reflection/serverreflection.go b/reflection/serverreflection.go index c3e63574..3c1fc9d7 100644 --- a/reflection/serverreflection.go +++ b/reflection/serverreflection.go @@ -228,7 +228,7 @@ func (s *serverReflectionServer) serviceMetadataForSymbol(name string) (interfac // Search for stream in info.Streams. for _, m := range info.Streams { - if m == name[pos+1:] { + if m.Name == name[pos+1:] { found = true break } diff --git a/server.go b/server.go index 413c019c..478277ea 100644 --- a/server.go +++ b/server.go @@ -245,12 +245,22 @@ func (s *Server) register(sd *ServiceDesc, ss interface{}) { s.m[sd.ServiceName] = srv } -// ServiceInfo contains unary rpc names, streaming rpc names and metadata for a service. +// StreamInfo contains information about a streaming RPC. +type StreamInfo struct { + // Name is the RPC 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 names, streaming RPC infos and metadata for a service. type ServiceInfo struct { - // Methods are unary rpc 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 + // Streams are streaming RPC names and streaming types. + Streams []*StreamInfo // Metadata is the metadata specified in ServiceDesc when registering service. Metadata interface{} } @@ -264,9 +274,13 @@ func (s *Server) GetServiceInfo() map[string]*ServiceInfo { for m := range srv.md { methods = append(methods, m) } - streams := make([]string, 0, len(srv.sd)) - for s := range srv.sd { - streams = append(streams, s) + streams := make([]*StreamInfo, 0, len(srv.sd)) + for s, d := range srv.sd { + streams = append(streams, &StreamInfo{ + Name: s, + IsClientStream: d.ClientStreams, + IsServerStream: d.ServerStreams, + }) } ret[n] = &ServiceInfo{ diff --git a/server_test.go b/server_test.go index d7bf2f98..d623764a 100644 --- a/server_test.go +++ b/server_test.go @@ -79,7 +79,7 @@ func TestGetServiceInfo(t *testing.T) { { StreamName: "EmptyStream", Handler: nil, - ServerStreams: true, + ServerStreams: false, ClientStreams: true, }, }, @@ -95,9 +95,11 @@ func TestGetServiceInfo(t *testing.T) { Methods: []string{ "EmptyCall", }, - Streams: []string{ - "EmptyStream", - }, + Streams: []*StreamInfo{&StreamInfo{ + Name: "EmptyStream", + IsClientStream: true, + IsServerStream: false, + }}, Metadata: []int{0, 2, 1, 3}, }, } From bc8885608f8d7ddd24cdaea8660a434591113037 Mon Sep 17 00:00:00 2001 From: Menghan Li Date: Mon, 11 Jul 2016 13:09:50 -0700 Subject: [PATCH 3/3] Merge unary RPC info and streaming RPC info --- reflection/serverreflection.go | 13 +------------ server.go | 29 ++++++++++++++--------------- server_test.go | 19 +++++++++++-------- 3 files changed, 26 insertions(+), 35 deletions(-) diff --git a/reflection/serverreflection.go b/reflection/serverreflection.go index 3c1fc9d7..0daa096f 100644 --- a/reflection/serverreflection.go +++ b/reflection/serverreflection.go @@ -214,20 +214,9 @@ func (s *serverReflectionServer) serviceMetadataForSymbol(name string) (interfac return nil, fmt.Errorf("unknown symbol: %v", name) } - // Search for method in info.Methods. + // Search for method name in info.Methods. var found bool for _, m := range info.Methods { - if m == name[pos+1:] { - found = true - break - } - } - if found { - return info.Metadata, nil - } - - // Search for stream in info.Streams. - for _, m := range info.Streams { if m.Name == name[pos+1:] { found = true break diff --git a/server.go b/server.go index 478277ea..f7b8319b 100644 --- a/server.go +++ b/server.go @@ -245,9 +245,9 @@ func (s *Server) register(sd *ServiceDesc, ss interface{}) { s.m[sd.ServiceName] = srv } -// StreamInfo contains information about a streaming RPC. -type StreamInfo struct { - // Name is the RPC name only, without the service name or package name. +// 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 @@ -255,12 +255,9 @@ type StreamInfo struct { IsServerStream bool } -// ServiceInfo contains unary RPC names, streaming RPC infos and metadata for a service. +// ServiceInfo contains unary RPC method info, streaming RPC methid info and metadata for a service. type ServiceInfo struct { - // Methods are unary RPC names only, without the service name or package name. - Methods []string - // Streams are streaming RPC names and streaming types. - Streams []*StreamInfo + Methods []MethodInfo // Metadata is the metadata specified in ServiceDesc when registering service. Metadata interface{} } @@ -270,14 +267,17 @@ 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)) + 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, + }) } - streams := make([]*StreamInfo, 0, len(srv.sd)) - for s, d := range srv.sd { - streams = append(streams, &StreamInfo{ - Name: s, + for m, d := range srv.sd { + methods = append(methods, MethodInfo{ + Name: m, IsClientStream: d.ClientStreams, IsServerStream: d.ServerStreams, }) @@ -285,7 +285,6 @@ func (s *Server) GetServiceInfo() map[string]*ServiceInfo { ret[n] = &ServiceInfo{ Methods: methods, - Streams: streams, Metadata: srv.mdata, } } diff --git a/server_test.go b/server_test.go index d623764a..eb598a51 100644 --- a/server_test.go +++ b/server_test.go @@ -92,14 +92,17 @@ func TestGetServiceInfo(t *testing.T) { info := server.GetServiceInfo() want := map[string]*ServiceInfo{ "grpc.testing.EmptyService": &ServiceInfo{ - Methods: []string{ - "EmptyCall", - }, - Streams: []*StreamInfo{&StreamInfo{ - Name: "EmptyStream", - IsClientStream: true, - IsServerStream: false, - }}, + Methods: []MethodInfo{ + MethodInfo{ + Name: "EmptyCall", + IsClientStream: false, + IsServerStream: false, + }, + MethodInfo{ + Name: "EmptyStream", + IsClientStream: true, + IsServerStream: false, + }}, Metadata: []int{0, 2, 1, 3}, }, }