-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathpool.go
316 lines (256 loc) · 7.63 KB
/
pool.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package connection
import (
"context"
"errors"
"fmt"
"sync"
"sync/atomic"
"time"
)
var ErrNoConnections = errors.New("no connections (online)")
type ConnectionFactoryFunc func(addr string) (*Connection, error)
type Pool struct {
Factory ConnectionFactoryFunc
Addrs []string
Opts PoolOptions
done chan struct{}
// WaitGroup to wait for all reconnect goroutines return
wg sync.WaitGroup
mu sync.Mutex // protects following fields
connections []*Connection
connIndex uint32
isClosed bool
}
// Pool - provides connections to the clients. It removes the connection from
// the pool if it was closed and in the background tries to create and etablish
// new connection so the pool will be full.
func NewPool(factory ConnectionFactoryFunc, addrs []string, options ...PoolOption) (*Pool, error) {
opts := GetDefaultPoolOptions()
for _, opt := range options {
if err := opt(&opts); err != nil {
return nil, fmt.Errorf("setting pool option: %v %w", opt, err)
}
}
return &Pool{
Factory: factory,
Opts: opts,
Addrs: addrs,
done: make(chan struct{}),
connections: make([]*Connection, 0),
}, nil
}
func (p *Pool) handleError(err error) {
if p.Opts.ErrorHandler == nil {
return
}
p.wg.Add(1)
go func() {
defer p.wg.Done()
p.Opts.ErrorHandler(err)
}()
}
// Connect creates poll of connections by calling Factory method and connect them all
func (p *Pool) Connect() error {
return p.ConnectCtx(context.Background())
}
// Connect creates poll of connections by calling Factory method and connect them all
func (p *Pool) ConnectCtx(ctx context.Context) error {
// We need to close pool (with all potentially running goroutines) if
// connection creation fails. Example of such situation is when we
// successfully created 2 connections, but 3rd failed and minimum
// connections is 3.
// Because `Close` uses same mutex as `Connect` we need to unlock it
// before calling `Close`. That's why we use `connectErr` variable and
// `defer` statement here, before the next `defer` which unlocks mutex.
var connectErr error
defer func() {
if connectErr != nil {
p.CloseCtx(ctx)
}
}()
p.mu.Lock()
defer p.mu.Unlock()
if p.isClosed {
return errors.New("pool is closed")
}
// errors from initial connections creation
var errs []error
// build connections
for _, addr := range p.Addrs {
conn, err := p.Factory(addr)
if err != nil {
return fmt.Errorf("creating connection for %s: %w", addr, err)
}
// set own handler when connection is closed
conn.SetOptions(ConnectionClosedHandler(p.handleClosedConnection))
err = conn.ConnectCtx(ctx)
if err != nil {
errs = append(errs, fmt.Errorf("connecting to %s: %w", addr, err))
p.handleError(fmt.Errorf("failed to connect to %s: %w", conn.addr, err))
p.wg.Add(1)
go p.recreateConnection(conn)
continue
}
p.connections = append(p.connections, conn)
}
if len(p.connections) >= p.Opts.MinConnections {
return nil
}
if len(errs) == 0 {
connectErr = fmt.Errorf("minimum %d connections is required, established: %d", p.Opts.MinConnections, len(p.connections))
} else {
connectErr = fmt.Errorf("minimum %d connections is required, established: %d, errors: %w", p.Opts.MinConnections, len(p.connections), errors.Join(errs...))
}
return connectErr
}
// Connections returns copy of all connections from the pool
func (p *Pool) Connections() []*Connection {
p.mu.Lock()
defer p.mu.Unlock()
// create new slice, as p.connections can be changed
var conns []*Connection
conns = append(conns, p.connections...)
return conns
}
// FilterFunc is a function to filter connections
type FilterFunc func(*Connection) bool
// Get returns filtered connection from the pool
func (p *Pool) Get() (*Connection, error) {
p.mu.Lock()
if p.isClosed {
p.mu.Unlock()
return nil, errors.New("pool is closed")
}
p.mu.Unlock()
// filtered connections
conns := p.filteredConnections()
if len(conns) == 0 {
return nil, ErrNoConnections
}
n := atomic.AddUint32(&p.connIndex, 1)
return conns[(int(n)-1)%len(conns)], nil
}
// when connection is closed, remove it from the pool of connections and start
// goroutine to create new connection for the same address
func (p *Pool) handleClosedConnection(closedConn *Connection) {
p.mu.Lock()
defer p.mu.Unlock()
if p.isClosed {
return
}
connIndex := -1
for i, conn := range p.connections {
if conn == closedConn {
connIndex = i
break
}
}
// somehow we didn't find closed connection in the pool
if connIndex < 0 {
p.handleError(errors.New("closed connection was not found in the pool"))
return
}
connsNum := len(p.connections)
p.connections[connIndex] = p.connections[connsNum-1] // Copy last element to index connIndex.
p.connections[connsNum-1] = nil // Erase last element
p.connections = p.connections[:connsNum-1] // Truncate slice.
// initiate goroutine to reconnect to closedConn.Addr
p.wg.Add(1)
go p.recreateConnection(closedConn)
}
func (p *Pool) recreateConnection(closedConn *Connection) {
defer p.wg.Done()
reconnectTime := p.Opts.ReconnectWait
for {
select {
case <-time.After(reconnectTime):
if p.Opts.MaxReconnectWait != 0 {
reconnectTime *= 2
if reconnectTime > p.Opts.MaxReconnectWait {
reconnectTime = p.Opts.MaxReconnectWait
}
}
case <-p.Done():
// if pool is closed, let's get out of here
return
}
conn, err := p.Factory(closedConn.addr)
if err != nil {
p.handleError(fmt.Errorf("failed to re-create connection for %s: %w", closedConn.addr, err))
return
}
// When connection is closed, remove it from the pool of connections and start
// recreate goroutine to create new connection for the same address
conn.SetOptions(ConnectionClosedHandler(p.handleClosedConnection))
// if we successfully reconnected, add connection to the pool and return
if err = conn.Connect(); err == nil {
p.mu.Lock()
p.connections = append(p.connections, conn)
p.mu.Unlock()
return
}
p.handleError(fmt.Errorf("failed to reconnect to %s: %w", conn.addr, err))
}
}
// Close closes all connections in the pool
func (p *Pool) Close() error {
return p.CloseCtx(context.Background())
}
// CloseCtx closes all connections in the pool
func (p *Pool) CloseCtx(ctx context.Context) error {
p.mu.Lock()
if p.isClosed {
p.mu.Unlock()
return nil
}
p.isClosed = true
p.mu.Unlock()
close(p.done)
// wait for all re-connection goroutines to stop
// they may add more connections to the pool
// so we want to be sure that no new connections will appear
// when we lock and start closing all connections
p.wg.Wait()
p.mu.Lock()
// close all connections concurrently
var wg sync.WaitGroup
wg.Add(len(p.connections))
for _, conn := range p.connections {
go func(conn *Connection) {
defer wg.Done()
err := conn.CloseCtx(ctx)
if err != nil {
p.handleError(fmt.Errorf("closing connection on pool close: %w", err))
}
}(conn)
}
wg.Wait()
// remove all connections
p.connections = make([]*Connection, 0)
p.mu.Unlock()
return nil
}
func (p *Pool) Done() <-chan struct{} {
return p.done
}
// filteredConnections returns filtered connections
func (p *Pool) filteredConnections() []*Connection {
if p.Opts.ConnectionsFilter == nil {
return p.Connections()
}
var conns []*Connection
for _, conn := range p.Connections() {
if p.Opts.ConnectionsFilter(conn) {
conns = append(conns, conn)
}
}
return conns
}
// IsDegraded returns true if pool is not full
func (p *Pool) IsDegraded() bool {
return len(p.Addrs) != len(p.filteredConnections())
}
// IsUp returns true if at least one connection is in the pool
func (p *Pool) IsUp() bool {
return len(p.filteredConnections()) > 0
}