mirror of https://github.com/grpc/grpc-go.git
Remove mutex and cache
This commit is contained in:
parent
a5a4628ee4
commit
864c571f44
|
|
@ -7,7 +7,6 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"reflect"
|
"reflect"
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/golang/protobuf/proto"
|
"github.com/golang/protobuf/proto"
|
||||||
dpb "github.com/golang/protobuf/protoc-gen-go/descriptor"
|
dpb "github.com/golang/protobuf/protoc-gen-go/descriptor"
|
||||||
|
|
@ -18,20 +17,13 @@ import (
|
||||||
|
|
||||||
type serverReflectionServer struct {
|
type serverReflectionServer struct {
|
||||||
s *grpc.Server
|
s *grpc.Server
|
||||||
// TODO mu is not used. Add lock() and unlock().
|
// TODO add cache if necessary
|
||||||
mu sync.Mutex
|
|
||||||
typeToNameMap map[reflect.Type]string
|
|
||||||
nameToTypeMap map[string]reflect.Type
|
|
||||||
typeToFileDescMap map[reflect.Type]*dpb.FileDescriptorProto
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// InstallOnServer installs server reflection service on the given grpc server.
|
// InstallOnServer installs server reflection service on the given grpc server.
|
||||||
func InstallOnServer(s *grpc.Server) {
|
func InstallOnServer(s *grpc.Server) {
|
||||||
rpb.RegisterServerReflectionServer(s, &serverReflectionServer{
|
rpb.RegisterServerReflectionServer(s, &serverReflectionServer{
|
||||||
s: s,
|
s: s,
|
||||||
typeToNameMap: make(map[reflect.Type]string),
|
|
||||||
nameToTypeMap: make(map[string]reflect.Type),
|
|
||||||
typeToFileDescMap: make(map[reflect.Type]*dpb.FileDescriptorProto),
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -48,18 +40,11 @@ func (s *serverReflectionServer) fileDescForType(st reflect.Type) (*dpb.FileDesc
|
||||||
}
|
}
|
||||||
enc, idxs := m.Descriptor()
|
enc, idxs := m.Descriptor()
|
||||||
|
|
||||||
// Check type to fileDesc cache.
|
|
||||||
if fd, ok := s.typeToFileDescMap[st]; ok {
|
|
||||||
return fd, idxs, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Cache missed, try to decode.
|
// Cache missed, try to decode.
|
||||||
fd, err := s.decodeFileDesc(enc)
|
fd, err := s.decodeFileDesc(enc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
// Add to cache.
|
|
||||||
s.typeToFileDescMap[st] = fd
|
|
||||||
return fd, idxs, nil
|
return fd, idxs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -91,30 +76,12 @@ func decompress(b []byte) []byte {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *serverReflectionServer) typeForName(name string) (reflect.Type, error) {
|
func (s *serverReflectionServer) typeForName(name string) (reflect.Type, error) {
|
||||||
// Check cache first.
|
|
||||||
if st, ok := s.nameToTypeMap[name]; ok {
|
|
||||||
return st, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
pt := proto.MessageType(name)
|
pt := proto.MessageType(name)
|
||||||
if pt == nil {
|
if pt == nil {
|
||||||
return nil, fmt.Errorf("unknown type: %q", name)
|
return nil, fmt.Errorf("unknown type: %q", name)
|
||||||
}
|
}
|
||||||
st := pt.Elem()
|
st := pt.Elem()
|
||||||
|
|
||||||
// Add to cache.
|
|
||||||
s.typeToNameMap[st] = name
|
|
||||||
s.nameToTypeMap[name] = st
|
|
||||||
|
|
||||||
// TODO is this necessary?
|
|
||||||
// In most cases, the returned type will be used to search
|
|
||||||
// for file descriptor.
|
|
||||||
// Add it to cache now.
|
|
||||||
fd, _, err := s.fileDescForType(st)
|
|
||||||
if err == nil {
|
|
||||||
s.typeToFileDescMap[st] = fd
|
|
||||||
}
|
|
||||||
|
|
||||||
return st, nil
|
return st, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -137,11 +104,6 @@ func (s *serverReflectionServer) fileDescContainingExtension(st reflect.Type, ex
|
||||||
}
|
}
|
||||||
|
|
||||||
extT := reflect.TypeOf(extDesc.ExtensionType).Elem()
|
extT := reflect.TypeOf(extDesc.ExtensionType).Elem()
|
||||||
// TODO this doesn't work if extT is simple types, like int32
|
|
||||||
// Check cache.
|
|
||||||
if fd, ok := s.typeToFileDescMap[extT]; ok {
|
|
||||||
return fd, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
fd, _, err := s.fileDescForType(extT)
|
fd, _, err := s.fileDescForType(extT)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/golang/protobuf/proto"
|
"github.com/golang/protobuf/proto"
|
||||||
dpb "github.com/golang/protobuf/protoc-gen-go/descriptor"
|
// dpb "github.com/golang/protobuf/protoc-gen-go/descriptor"
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
rpb "google.golang.org/grpc/reflection/grpc_reflection_v1alpha"
|
rpb "google.golang.org/grpc/reflection/grpc_reflection_v1alpha"
|
||||||
|
|
@ -16,12 +16,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
s = &serverReflectionServer{
|
s = &serverReflectionServer{}
|
||||||
// s: s,
|
|
||||||
typeToNameMap: make(map[reflect.Type]string),
|
|
||||||
nameToTypeMap: make(map[string]reflect.Type),
|
|
||||||
typeToFileDescMap: make(map[reflect.Type]*dpb.FileDescriptorProto),
|
|
||||||
}
|
|
||||||
// fileDescriptorX of each test proto file.
|
// fileDescriptorX of each test proto file.
|
||||||
fdTest []byte
|
fdTest []byte
|
||||||
fdProto2 []byte
|
fdProto2 []byte
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue