-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
66 lines (58 loc) · 1.56 KB
/
util.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
package totem
import (
"fmt"
"sort"
"strings"
"time"
"github.com/jhump/protoreflect/grpcreflect"
"google.golang.org/grpc"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/descriptorpb"
)
var ErrTimeout = fmt.Errorf("timed out")
func WaitErrOrTimeout(errC <-chan error, timeout time.Duration) error {
select {
case err := <-errC:
return err
case <-time.After(timeout):
return ErrTimeout
}
}
func LoadServiceDesc(svc *grpc.ServiceDesc) (*descriptorpb.ServiceDescriptorProto, error) {
desc, err := grpcreflect.LoadServiceDescriptor(svc)
if err != nil {
return nil, err
}
dpb := proto.Clone(desc.AsServiceDescriptorProto()).(*descriptorpb.ServiceDescriptorProto)
fqn := desc.GetFullyQualifiedName()
dpb.Name = &fqn
return dpb, nil
}
func (i *ServiceInfo) MethodNames() []string {
var names []string
for _, svc := range i.GetServices() {
for _, m := range svc.GetMethod() {
names = append(names, fmt.Sprintf("/%s/%s", svc.GetName(), m.GetName()))
}
}
sort.Strings(names)
return names
}
// parses a method name of the form "/service/method"
func parseQualifiedMethod(method string) (string, string, error) {
parts := strings.Split(method, "/")
if len(parts) != 3 {
return "", "", fmt.Errorf("invalid method name: %s", method)
}
return parts[1], parts[2], nil
}
func (r *RPC) QualifiedMethodName() string {
switch r.GetContent().(type) {
case *RPC_Request:
return fmt.Sprintf("/%s/%s", r.GetServiceName(), r.GetMethodName())
case *RPC_Response:
return fmt.Sprintf("(response)")
default:
return fmt.Sprintf("(unknown)")
}
}