/
connection.go
55 lines (50 loc) · 1.43 KB
/
connection.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
package cli
import (
"crypto/tls"
"fmt"
"net/url"
"strings"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
const (
shortConnectionTimeout = time.Second
longConnectionTimeout = 10 * time.Second
)
// if the address is a local one, return a short timeout, else return a longer timeout
func getTimeout(addr string) (time.Duration, error) {
// a parsable URL needs a scheme, just add it if it's not there
if !strings.Contains(addr, "://") {
addr = fmt.Sprintf("scheme://%s", addr)
}
u, err := url.Parse(addr)
if err != nil {
return 0, err
}
host := u.Hostname()
switch host {
case "localhost", "127.0.0.1", "::1":
return shortConnectionTimeout, nil
default:
return longConnectionTimeout, nil
}
}
// NewClientConn is a helper function that wraps the steps involved in setting up a grpc client connection to the API.
func NewClientConn(addr string, token string, skipVerify bool) (*grpc.ClientConn, error) {
tlsConfig := &tls.Config{InsecureSkipVerify: skipVerify}
creds := credentials.NewTLS(tlsConfig)
connectionTimeout, err := getTimeout(addr)
if err != nil {
return nil, err
}
opts := []grpc.DialOption{
grpc.WithTransportCredentials(creds),
grpc.WithBlock(),
grpc.WithTimeout(connectionTimeout),
grpc.WithPerRPCCredentials(&LoginCredentials{Token: token}),
grpc.WithCompressor(grpc.NewGZIPCompressor()),
grpc.WithDecompressor(grpc.NewGZIPDecompressor()),
}
return grpc.Dial(addr, opts...)
}