-
Notifications
You must be signed in to change notification settings - Fork 2
/
client_conn.go
171 lines (132 loc) · 4.64 KB
/
client_conn.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package client
import (
"context"
"crypto/tls"
"crypto/x509"
"io/ioutil"
"strconv"
"github.com/americanas-go/log"
"google.golang.org/grpc"
"google.golang.org/grpc/backoff"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/keepalive"
)
// Plugin defines a function to process plugin.
type Plugin func(ctx context.Context) ([]grpc.DialOption, []grpc.CallOption)
// NewClientConn returns a new grpc client connection.
func NewClientConn(ctx context.Context, plugins ...Plugin) *grpc.ClientConn {
opt, err := NewOptions()
if err != nil {
panic(err)
}
return NewClientConnWithOptions(ctx, opt, plugins...)
}
// NewClientConnWithConfigPath returns a new grpc client connection with options from config path.
func NewClientConnWithConfigPath(ctx context.Context, path string) (*grpc.ClientConn, error) {
options, err := NewOptionsWithPath(path)
if err != nil {
return nil, err
}
return NewClientConnWithOptions(ctx, options), nil
}
// NewClientConnWithOptions returns a new grpc client connection with options.
func NewClientConnWithOptions(ctx context.Context, options *Options, plugins ...Plugin) *grpc.ClientConn {
var err error
var conn *grpc.ClientConn
var opts []grpc.DialOption
logger := log.FromContext(ctx)
serverAddr := options.Host + ":" + strconv.Itoa(options.Port)
if options.TLS.Enabled {
logger.Tracef("creating TLS grpc client for host %s", serverAddr)
opts = addTlsOptions(ctx, options, opts)
} else {
logger.Tracef("creating insecure grpc client for host %s", serverAddr)
opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
}
callOpts := make([]grpc.CallOption, 0)
opts = append(opts, grpc.WithInitialWindowSize(options.InitialWindowSize))
opts = append(opts, grpc.WithInitialConnWindowSize(options.InitialConnWindowSize))
if options.Block {
opts = append(opts, grpc.WithBlock())
}
if options.HostOverwrite != "" {
opts = append(opts, grpc.WithAuthority(options.HostOverwrite))
}
opts = append(opts, grpc.WithConnectParams(grpc.ConnectParams{
Backoff: backoff.Config{
BaseDelay: options.ConnectParams.Backoff.BaseDelay,
Multiplier: options.ConnectParams.Backoff.Multiplier,
Jitter: options.ConnectParams.Backoff.Jitter,
MaxDelay: options.ConnectParams.Backoff.MaxDelay,
},
MinConnectTimeout: options.ConnectParams.MinConnectTimeout,
}))
opts = append(opts, grpc.WithKeepaliveParams(keepalive.ClientParameters{
Time: options.Keepalive.Time,
Timeout: options.Keepalive.Timeout,
PermitWithoutStream: options.Keepalive.PermitWithoutStream,
}))
for _, plugin := range plugins {
dopts, copts := plugin(ctx)
if dopts != nil {
opts = append(opts, dopts...)
}
if copts != nil {
callOpts = append(callOpts, copts...)
}
}
if len(callOpts) > 0 {
opts = append(opts, grpc.WithDefaultCallOptions(callOpts...))
}
conn, err = grpc.Dial(serverAddr, opts...)
if err != nil {
logger.Fatalf("fail to dial: %v", err)
return nil
}
logger.Debugf("grpc client created for host %s", serverAddr)
return conn
}
func addTlsOptions(ctx context.Context, opt *Options, opts []grpc.DialOption) []grpc.DialOption {
logger := log.FromContext(ctx)
var creds credentials.TransportCredentials
options := opt.TLS
if options.CertFile != "" && options.KeyFile != "" {
// Load the client certificates from disk
cert, err := tls.LoadX509KeyPair(options.CertFile, options.KeyFile)
if err != nil {
logger.Fatalf("could not load client key pair: %s", err)
}
if options.CAFile != "" {
// Create a certificate pool from the certificate authority
certPool := x509.NewCertPool()
ca, err := ioutil.ReadFile(options.CAFile)
if err != nil {
logger.Fatalf("could not read ca certificate: %s", err)
}
// Append the certificates from the CA
if ok := certPool.AppendCertsFromPEM(ca); !ok {
logger.Fatalf("failed to append ca certs")
}
creds = credentials.NewTLS(&tls.Config{
ServerName: opt.Host, // NOTE: this is required!
Certificates: []tls.Certificate{cert},
RootCAs: certPool,
InsecureSkipVerify: options.InsecureSkipVerify,
})
} else {
creds = credentials.NewTLS(&tls.Config{
ServerName: opt.Host, // NOTE: this is required!
Certificates: []tls.Certificate{cert},
InsecureSkipVerify: options.InsecureSkipVerify,
})
}
} else {
creds = credentials.NewTLS(&tls.Config{
ServerName: opt.Host, // NOTE: this is required!
Certificates: []tls.Certificate{},
InsecureSkipVerify: options.InsecureSkipVerify,
})
}
return append(opts, grpc.WithTransportCredentials(creds))
}