-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient.go
43 lines (37 loc) · 1016 Bytes
/
client.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
package channelz
import (
"fmt"
"strings"
"github.com/pkg/errors"
"google.golang.org/grpc"
channelzgrpc "google.golang.org/grpc/channelz/grpc_channelz_v1"
)
func (h *grpcChannelzHandler) connect() (channelzgrpc.ChannelzClient, error) {
if h.client != nil {
// Already connected
return h.client, nil
}
host := getHostFromBindAddress(h.bindAddress)
h.mu.Lock()
defer h.mu.Unlock()
client, err := newChannelzClient(host, h.dialOpts...)
if err != nil {
return nil, err
}
h.client = client
return h.client, nil
}
func newChannelzClient(dialString string, opts ...grpc.DialOption) (channelzgrpc.ChannelzClient, error) {
conn, err := grpc.Dial(dialString, opts...)
if err != nil {
return nil, errors.Wrapf(err, "error dialing to %s", dialString)
}
client := channelzgrpc.NewChannelzClient(conn)
return client, nil
}
func getHostFromBindAddress(bindAddress string) string {
if strings.HasPrefix(bindAddress, ":") {
return fmt.Sprintf("localhost%s", bindAddress)
}
return bindAddress
}