-
Notifications
You must be signed in to change notification settings - Fork 110
/
rpc.go
84 lines (75 loc) · 1.89 KB
/
rpc.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Package testutils implements test utilities.
package testutils
import (
"context"
"sync"
"go.viam.com/utils/rpc"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
// TrackingDialer tracks dial attempts.
type TrackingDialer struct {
rpc.Dialer
NewConnections int
}
// DialDirect tracks calls of DialDirect.
func (td *TrackingDialer) DialDirect(
ctx context.Context,
target string,
keyExtra string,
onClose func() error,
opts ...grpc.DialOption,
) (rpc.ClientConn, bool, error) {
conn, cached, err := td.Dialer.DialDirect(ctx, target, keyExtra, onClose, opts...)
if err != nil {
return nil, false, err
}
if !cached {
td.NewConnections++
}
return conn, cached, err
}
// DialFunc tracks calls of DialFunc.
func (td *TrackingDialer) DialFunc(
proto string,
target string,
keyExtra string,
f func() (rpc.ClientConn, func() error, error),
) (rpc.ClientConn, bool, error) {
conn, cached, err := td.Dialer.DialFunc(proto, target, keyExtra, f)
if err != nil {
return nil, false, err
}
if !cached {
td.NewConnections++
}
return conn, cached, err
}
// ServerTransportStream implements grpc.ServerTransportStream and can be used to test setting
// metadata in the gRPC response header.
type ServerTransportStream struct {
mu sync.Mutex
grpc.ServerTransportStream
md metadata.MD
}
// NewServerTransportStream creates a new ServerTransportStream.
func NewServerTransportStream() *ServerTransportStream {
return &ServerTransportStream{
md: metadata.New(make(map[string]string)),
}
}
// SetHeader implements grpc.ServerTransportStream.
func (s *ServerTransportStream) SetHeader(md metadata.MD) error {
s.mu.Lock()
defer s.mu.Unlock()
for k, v := range md {
s.md[k] = v
}
return nil
}
// Value returns the value in the metadata map corresponding to a given key.
func (s *ServerTransportStream) Value(key string) []string {
s.mu.Lock()
defer s.mu.Unlock()
return s.md[key]
}