-
Notifications
You must be signed in to change notification settings - Fork 71
/
connection_pool.go
352 lines (306 loc) · 9.36 KB
/
connection_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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
*
* Copyright (c) 2020 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License.
*
*/
package nebula_go
import (
"container/list"
"crypto/tls"
"fmt"
"sync"
"time"
"github.com/vesoft-inc/nebula-go/v2/nebula"
)
type ConnectionPool struct {
idleConnectionQueue list.List
activeConnectionQueue list.List
addresses []HostAddress
conf PoolConfig
hostIndex int
log Logger
rwLock sync.RWMutex
cleanerChan chan struct{} //notify when pool is close
closed bool
sslConfig *tls.Config
}
// NewConnectionPool constructs a new connection pool using the given addresses and configs
func NewConnectionPool(addresses []HostAddress, conf PoolConfig, log Logger) (*ConnectionPool, error) {
return NewSslConnectionPool(addresses, conf, nil, log)
}
// NewConnectionPool constructs a new SSL connection pool using the given addresses and configs
func NewSslConnectionPool(addresses []HostAddress, conf PoolConfig, sslConfig *tls.Config, log Logger) (*ConnectionPool, error) {
// Process domain to IP
convAddress, err := DomainToIP(addresses)
if err != nil {
return nil, fmt.Errorf("failed to find IP, error: %s ", err.Error())
}
// Check input
if len(convAddress) == 0 {
return nil, fmt.Errorf("failed to initialize connection pool: illegal address input")
}
// Check config
conf.validateConf(log)
newPool := &ConnectionPool{
conf: conf,
log: log,
addresses: convAddress,
hostIndex: 0,
sslConfig: sslConfig,
}
// Init pool with SSL socket
if err = newPool.initPool(); err != nil {
return nil, err
}
newPool.startCleaner()
return newPool, nil
}
// initPool initializes the connection pool
func (pool *ConnectionPool) initPool() error {
if err := pool.checkAddresses(); err != nil {
return fmt.Errorf("failed to open connection, error: %s ", err.Error())
}
for i := 0; i < pool.conf.MinConnPoolSize; i++ {
// Simple round-robin
newConn := newConnection(pool.addresses[i%len(pool.addresses)])
// Open connection to host
if err := newConn.open(newConn.severAddress, pool.conf.TimeOut, pool.sslConfig); err != nil {
// If initialization failed, clean idle queue
idleLen := pool.idleConnectionQueue.Len()
for i := 0; i < idleLen; i++ {
pool.idleConnectionQueue.Front().Value.(*connection).close()
pool.idleConnectionQueue.Remove(pool.idleConnectionQueue.Front())
}
return fmt.Errorf("failed to open connection, error: %s ", err.Error())
}
// Mark connection as in use
pool.idleConnectionQueue.PushBack(newConn)
}
return nil
}
// GetSession authenticates the username and password.
// It returns a session if the authentication succeed.
func (pool *ConnectionPool) GetSession(username, password string) (*Session, error) {
// Get valid and usable connection
var conn *connection = nil
var err error = nil
const retryTimes = 3
for i := 0; i < retryTimes; i++ {
conn, err = pool.getIdleConn()
if err == nil {
break
}
}
if conn == nil {
return nil, err
}
// Authenticate
resp, err := conn.authenticate(username, password)
if err != nil || resp.GetErrorCode() != nebula.ErrorCode_SUCCEEDED {
// if authentication failed, put connection back
pool.rwLock.Lock()
defer pool.rwLock.Unlock()
removeFromList(&pool.activeConnectionQueue, conn)
pool.idleConnectionQueue.PushBack(conn)
return nil, err
}
sessID := resp.GetSessionID()
timezoneOffset := resp.GetTimeZoneOffsetSeconds()
timezoneName := resp.GetTimeZoneName()
// Create new session
newSession := Session{
sessionID: sessID,
connection: conn,
connPool: pool,
log: pool.log,
timezoneInfo: timezoneInfo{timezoneOffset, timezoneName},
}
return &newSession, nil
}
func (pool *ConnectionPool) getIdleConn() (*connection, error) {
pool.rwLock.Lock()
defer pool.rwLock.Unlock()
// Take an idle valid connection if possible
if pool.idleConnectionQueue.Len() > 0 {
var newConn *connection = nil
var newEle *list.Element = nil
for ele := pool.idleConnectionQueue.Front(); ele != nil; ele = ele.Next() {
// Check if connection is valid
if res := ele.Value.(*connection).ping(); res {
newConn = ele.Value.(*connection)
newEle = ele
break
}
}
if newConn == nil {
return pool.createConnection()
}
// Remove new connection from idle and add to active if found
pool.idleConnectionQueue.Remove(newEle)
pool.activeConnectionQueue.PushBack(newConn)
return newConn, nil
}
// Create a new connection if there is no idle connection and total connection < pool max size
newConn, err := pool.createConnection()
// TODO: If no idle avaliable, wait for timeout and reconnect
return newConn, err
}
// Release connection to pool
func (pool *ConnectionPool) release(conn *connection) {
pool.rwLock.Lock()
defer pool.rwLock.Unlock()
// Remove connection from active queue and add into idle queue
removeFromList(&pool.activeConnectionQueue, conn)
conn.release()
pool.idleConnectionQueue.PushBack(conn)
}
// Ping checks avaliability of host
func (pool *ConnectionPool) Ping(host HostAddress, timeout time.Duration) error {
newConn := newConnection(host)
// Open connection to host
if err := newConn.open(newConn.severAddress, timeout, pool.sslConfig); err != nil {
return err
}
newConn.close()
return nil
}
// Close closes all connection
func (pool *ConnectionPool) Close() {
pool.rwLock.Lock()
defer pool.rwLock.Unlock()
idleLen := pool.idleConnectionQueue.Len()
activeLen := pool.activeConnectionQueue.Len()
for i := 0; i < idleLen; i++ {
pool.idleConnectionQueue.Front().Value.(*connection).close()
pool.idleConnectionQueue.Remove(pool.idleConnectionQueue.Front())
}
for i := 0; i < activeLen; i++ {
pool.activeConnectionQueue.Front().Value.(*connection).close()
pool.activeConnectionQueue.Remove(pool.activeConnectionQueue.Front())
}
pool.closed = true
if pool.cleanerChan != nil {
close(pool.cleanerChan)
}
}
func (pool *ConnectionPool) getActiveConnCount() int {
return pool.activeConnectionQueue.Len()
}
func (pool *ConnectionPool) getIdleConnCount() int {
return pool.idleConnectionQueue.Len()
}
// Get a valid host (round robin)
func (pool *ConnectionPool) getHost() HostAddress {
if pool.hostIndex == len(pool.addresses) {
pool.hostIndex = 0
}
host := pool.addresses[pool.hostIndex]
pool.hostIndex++
return host
}
// Select a new host to create a new connection
func (pool *ConnectionPool) newConnToHost() (*connection, error) {
// Get a valid host (round robin)
host := pool.getHost()
newConn := newConnection(host)
// Open connection to host
if err := newConn.open(newConn.severAddress, pool.conf.TimeOut, pool.sslConfig); err != nil {
return nil, err
}
// Add connection to active queue
pool.activeConnectionQueue.PushBack(newConn)
// TODO: update workload
return newConn, nil
}
// Remove a connection from list
func removeFromList(l *list.List, conn *connection) {
for ele := l.Front(); ele != nil; ele = ele.Next() {
if ele.Value.(*connection) == conn {
l.Remove(ele)
}
}
}
// Compare total connection number with pool max size and return a connection if capable
func (pool *ConnectionPool) createConnection() (*connection, error) {
totalConn := pool.idleConnectionQueue.Len() + pool.activeConnectionQueue.Len()
// If no idle avaliable and the number of total connection reaches the max pool size, return error/wait for timeout
if totalConn >= pool.conf.MaxConnPoolSize {
return nil, fmt.Errorf("failed to get connection: No valid connection" +
" in the idle queue and connection number has reached the pool capacity")
}
newConn, err := pool.newConnToHost()
if err != nil {
return nil, err
}
// TODO: update workload
return newConn, nil
}
// startCleaner starts connectionCleaner if idleTime > 0.
func (pool *ConnectionPool) startCleaner() {
if pool.conf.IdleTime > 0 && pool.cleanerChan == nil {
pool.cleanerChan = make(chan struct{}, 1)
go pool.connectionCleaner()
}
}
func (pool *ConnectionPool) connectionCleaner() {
const minInterval = time.Minute
d := pool.conf.IdleTime
if d < minInterval {
d = minInterval
}
t := time.NewTimer(d)
for {
select {
case <-t.C:
case <-pool.cleanerChan: // pool was closed.
}
pool.rwLock.Lock()
if pool.closed {
pool.cleanerChan = nil
pool.rwLock.Unlock()
return
}
closing := pool.timeoutConnectionList()
pool.rwLock.Unlock()
for _, c := range closing {
c.close()
}
t.Reset(d)
}
}
func (pool *ConnectionPool) timeoutConnectionList() (closing []*connection) {
if pool.conf.IdleTime > 0 {
expiredSince := time.Now().Add(-pool.conf.IdleTime)
var newEle *list.Element = nil
maxCleanSize := pool.idleConnectionQueue.Len() + pool.activeConnectionQueue.Len() - pool.conf.MinConnPoolSize
for ele := pool.idleConnectionQueue.Front(); ele != nil; {
if maxCleanSize == 0 {
return
}
newEle = ele.Next()
// Check connection is expired
if !ele.Value.(*connection).returnedAt.Before(expiredSince) {
return
}
closing = append(closing, ele.Value.(*connection))
pool.idleConnectionQueue.Remove(ele)
ele = newEle
maxCleanSize--
}
}
return
}
func (pool *ConnectionPool) checkAddresses() error {
var timeout = 3 * time.Second
if pool.conf.TimeOut != 0 && pool.conf.TimeOut < timeout {
timeout = pool.conf.TimeOut
}
for _, address := range pool.addresses {
if err := pool.Ping(address, timeout); err != nil {
return err
}
}
return nil
}