/
client.go
executable file
·64 lines (54 loc) · 1.39 KB
/
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package yb
import (
"crypto/tls"
"log"
"time"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
//Client wrapper for YB
type Client interface {
Close()
V2() YBClient
Context() context.Context
}
type grpcClient struct {
Controller string
Conn *grpc.ClientConn
Cancel context.CancelFunc
YB YBClient
Ctx context.Context
}
func (client *grpcClient) Close() {
client.Conn.Close()
client.Cancel()
}
func (client *grpcClient) V2() YBClient {
return client.YB
}
func (client *grpcClient) Context() context.Context {
return client.Ctx
}
//Connect init a connection to grpc server
func Connect(host string, perRPC credentials.PerRPCCredentials, timeout time.Duration) Client {
client := new(grpcClient)
client.Controller = host
opts := []grpc.DialOption{
// See: https://godoc.org/google.golang.org/grpc#PerRPCCredentials
grpc.WithPerRPCCredentials(perRPC),
// oauth.NewOauthAccess requires the configuration of transport credentials.
grpc.WithTransportCredentials(
//TODO skip this for now
credentials.NewTLS(&tls.Config{InsecureSkipVerify: true}),
),
}
conn, err := grpc.Dial(client.Controller, opts...)
if err != nil {
log.Fatalf("Did not connect: %v", err)
}
client.Conn = conn
client.YB = NewYBClient(client.Conn)
client.Ctx, client.Cancel = context.WithTimeout(context.Background(), timeout)
return client
}