/
client.go
201 lines (180 loc) · 5.56 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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright 2017-2019 Lei Ni (nilei81@gmail.com)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
Package client contains RPC client functions and structs.
*/
package client
import (
"context"
"crypto/tls"
"sync"
"sync/atomic"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"github.com/lni/dragonboat/internal/settings"
"github.com/lni/dragonboat/internal/utils/compression"
"github.com/lni/dragonboat/internal/utils/netutil"
"github.com/lni/dragonboat/logger"
)
var (
plog = logger.GetLogger("drummer/client")
)
// Connection is a gRPC client connection.
type Connection struct {
connection *grpc.ClientConn
closed uint32
}
// ClientConn returns the gRPC client connection.
func (c *Connection) ClientConn() *grpc.ClientConn {
return c.connection
}
// Close closes the connection.
func (c *Connection) Close() {
atomic.StoreUint32(&c.closed, 1)
if err := c.connection.Close(); err != nil {
plog.Errorf("failed to close the connection")
}
}
// IsClosed returns whether the connection has been closed or not.
func (c *Connection) IsClosed() bool {
return atomic.LoadUint32(&c.closed) == 1
}
type addDialOptionsFunc func([]grpc.DialOption) []grpc.DialOption
// Pool manages a pool of gRPC client connections keyed by their
// destination addresses.
type Pool struct {
mu sync.Mutex
addOptions addDialOptionsFunc
connections map[string]*Connection
}
// NewConnectionPool creates a new object.
func NewConnectionPool() *Pool {
p := &Pool{
connections: make(map[string]*Connection),
}
return p
}
// NewDrummerConnectionPool returns a gRPC connection pool with all
// connections configured for use with Drummer servers.
func NewDrummerConnectionPool() *Pool {
p := NewConnectionPool()
p.SetAddDialOptionsFunc(drummerExtraDialOptions)
return p
}
func drummerExtraDialOptions(opts []grpc.DialOption) []grpc.DialOption {
co1 := grpc.MaxCallRecvMsgSize(int(settings.Soft.MaxDrummerClientMsgSize))
co2 := grpc.MaxCallSendMsgSize(int(settings.Soft.MaxDrummerClientMsgSize))
co3 := grpc.WithCompressor(compression.NewCompressor())
co4 := grpc.WithDecompressor(compression.NewDecompressor())
opts = append(opts, grpc.WithDefaultCallOptions(co1, co2))
opts = append(opts, co3)
opts = append(opts, co4)
return opts
}
// Close closes all connections in the pool.
func (p *Pool) Close() {
p.mu.Lock()
defer p.mu.Unlock()
for _, c := range p.connections {
c.Close()
}
p.connections = make(map[string]*Connection)
}
// GetConnection tries to get a connection based on the specified TLS config.
// The specified tlsConfig should be configured to use TLS mutual
// authentication or it should be nil for using insecure connection.
func (p *Pool) GetConnection(ctx context.Context, addr string,
tlsConfig *tls.Config) (*Connection, error) {
return p.getConnection(ctx, addr, tlsConfig)
}
// GetInsecureConnection returns an insecure Connection instance connected to
// the specified grpc server. It is recommended to use TLS connection for
// secured communication.
func (p *Pool) GetInsecureConnection(ctx context.Context,
addr string) (*Connection, error) {
return p.getConnection(ctx, addr, nil)
}
// GetTLSConnection returns an TLS connection connected to the specified grpc
// server.
func (p *Pool) GetTLSConnection(ctx context.Context, addr string,
caFile string, certFile string, keyFile string) (*Connection, error) {
hostname, err := netutil.GetHost(addr)
if err != nil {
return nil, err
}
tlsConfig, err := netutil.GetClientTLSConfig(hostname,
caFile, certFile, keyFile)
if err != nil {
return nil, err
}
if tlsConfig == nil {
plog.Panicf("tlsConfig not expected to be nil")
}
return p.getConnection(ctx, addr, tlsConfig)
}
// SetAddDialOptionsFunc sets the func used for adding extra connection options.
func (p *Pool) SetAddDialOptionsFunc(f addDialOptionsFunc) {
p.addOptions = f
}
func (p *Pool) getConnection(ctx context.Context, addr string,
tlsConfig *tls.Config) (*Connection, error) {
p.mu.Lock()
defer p.mu.Unlock()
c, ok := p.connections[addr]
if ok && !c.IsClosed() {
return c, nil
}
grpcConn, err := p.newConnection(ctx, addr, tlsConfig)
if err != nil {
return nil, err
}
c = &Connection{
connection: grpcConn,
}
p.connections[addr] = c
tt := "insecure"
if tlsConfig != nil {
tt = "TLS"
}
plog.Infof("connected to %s using %s transport", addr, tt)
return c, nil
}
func (p *Pool) newConnection(ctx context.Context,
addr string, tlsConfig *tls.Config) (*grpc.ClientConn, error) {
var C *grpc.ClientConn
if err := func() error {
var opts []grpc.DialOption
if tlsConfig == nil {
opts = append(opts, grpc.WithInsecure())
} else {
tc := grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig))
opts = append(opts, tc)
}
opts = append(opts, grpc.WithBlock())
if p.addOptions != nil {
opts = p.addOptions(opts)
}
conn, err := grpc.DialContext(ctx, addr, opts...)
if err != nil {
return err
}
C = conn
plog.Infof("connection to RPC server %s established", addr)
return nil
}(); err != nil {
plog.Warningf("failed to connect to RPC server %s, %v", addr, err)
return nil, err
}
return C, nil
}