forked from fullstorydev/grpcui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
methods.go
67 lines (63 loc) · 2.18 KB
/
methods.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package grpcui
import (
"github.com/jhump/protoreflect/desc"
"github.com/jhump/protoreflect/grpcreflect"
"golang.org/x/net/context"
"google.golang.org/grpc"
rpb "google.golang.org/grpc/reflection/grpc_reflection_v1alpha"
)
// AllMethodsForServices returns a slice that contains the method descriptors
// for all methods in the given services.
func AllMethodsForServices(descs []*desc.ServiceDescriptor) []*desc.MethodDescriptor {
seen := map[string]struct{}{}
var allMethods []*desc.MethodDescriptor
for _, sd := range descs {
if _, ok := seen[sd.GetFullyQualifiedName()]; ok {
// duplicate
continue
}
seen[sd.GetFullyQualifiedName()] = struct{}{}
allMethods = append(allMethods, sd.GetMethods()...)
}
return allMethods
}
// AllMethodsForServer returns a slice that contains the method descriptors for
// all methods exposed by the given gRPC server.
func AllMethodsForServer(svr *grpc.Server) ([]*desc.MethodDescriptor, error) {
svcs, err := grpcreflect.LoadServiceDescriptors(svr)
if err != nil {
return nil, err
}
var descs []*desc.ServiceDescriptor
for _, sd := range svcs {
descs = append(descs, sd)
}
return AllMethodsForServices(descs), nil
}
// AllMethodsViaReflection returns a slice that contains the method descriptors
// for all methods exposed by the server on the other end of the given
// connection. This returns an error if the server does not support service
// reflection. (See "google.golang.org/grpc/reflection" for more on service
// reflection.)
// This automatically skips the reflection service, since it is assumed this is not
// a desired inclusion.
func AllMethodsViaReflection(ctx context.Context, cc *grpc.ClientConn) ([]*desc.MethodDescriptor, error) {
stub := rpb.NewServerReflectionClient(cc)
cli := grpcreflect.NewClient(ctx, stub)
svcNames, err := cli.ListServices()
if err != nil {
return nil, err
}
var descs []*desc.ServiceDescriptor
for _, svcName := range svcNames {
sd, err := cli.ResolveService(svcName)
if err != nil {
return nil, err
}
if sd.GetFullyQualifiedName() == "grpc.reflection.v1alpha.ServerReflection" {
continue // skip reflection service
}
descs = append(descs, sd)
}
return AllMethodsForServices(descs), nil
}