-
Notifications
You must be signed in to change notification settings - Fork 110
/
rpc.go
50 lines (45 loc) · 1.02 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
// Package testutils implements test utilities.
package testutils
import (
"context"
"go.viam.com/utils/rpc"
"google.golang.org/grpc"
)
// 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
}