Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reflection: improve server implementation #5197

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
no need for sync.Once
  • Loading branch information
jhump committed Feb 18, 2022
commit a97ada894ee90340c4b53e0d0db7a7ec108e4283
42 changes: 14 additions & 28 deletions reflection/serverreflection.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ package reflection // import "google.golang.org/grpc/reflection"
import (
"io"
"sort"
"sync"

"google.golang.org/grpc"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -82,9 +81,6 @@ type serverReflectionServer struct {
s ServiceInfoProvider
descResolver protodesc.Resolver
extResolver ExtensionResolver

initServiceNames sync.Once
serviceNames []string
}

// ServerOptions represents the options used to construct a reflection server.
Expand Down Expand Up @@ -139,21 +135,6 @@ func Register(s GRPCServer) {
rpb.RegisterServerReflectionServer(s, svr)
}

func (s *serverReflectionServer) init() {
s.initServiceNames.Do(func() {
if s.s == nil {
// no need to init; no service names advertised
return
}
serviceInfo := s.s.GetServiceInfo()
s.serviceNames = make([]string, 0, len(serviceInfo))
for svc := range serviceInfo {
s.serviceNames = append(s.serviceNames, svc)
}
sort.Strings(s.serviceNames)
})
}

// fileDescWithDependencies returns a slice of serialized fileDescriptors in
// wire format ([]byte). The fileDescriptors will include fd and all the
// transitive dependencies of fd with names not in sentFileDescriptors.
Expand Down Expand Up @@ -221,10 +202,21 @@ func (s *serverReflectionServer) allExtensionNumbersForTypeName(name string) ([]
return numbers, nil
}

// listServices returns the names of services this server exposes.
func (s *serverReflectionServer) listServices() []*rpb.ServiceResponse {
serviceInfo := s.s.GetServiceInfo()
resp := make([]*rpb.ServiceResponse, 0, len(serviceInfo))
for svc := range serviceInfo {
resp = append(resp, &rpb.ServiceResponse{Name: svc})
}
sort.Slice(resp, func(i, j int) bool {
return resp[i].Name < resp[j].Name
})
return resp
}

// ServerReflectionInfo is the reflection service handler.
func (s *serverReflectionServer) ServerReflectionInfo(stream rpb.ServerReflection_ServerReflectionInfoServer) error {
s.init()

sentFileDescriptors := make(map[string]bool)
for {
in, err := stream.Recv()
Expand Down Expand Up @@ -306,15 +298,9 @@ func (s *serverReflectionServer) ServerReflectionInfo(stream rpb.ServerReflectio
}
}
case *rpb.ServerReflectionRequest_ListServices:
serviceResponses := make([]*rpb.ServiceResponse, len(s.serviceNames))
for i, n := range s.serviceNames {
serviceResponses[i] = &rpb.ServiceResponse{
Name: n,
}
}
out.MessageResponse = &rpb.ServerReflectionResponse_ListServicesResponse{
ListServicesResponse: &rpb.ListServiceResponse{
Service: serviceResponses,
Service: s.listServices(),
},
}
default:
Expand Down