/
pool.go
557 lines (515 loc) · 13.1 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
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
package ssh
import (
"context"
"fmt"
"golang.org/x/crypto/ssh"
"log"
"net"
"sync"
"sync/atomic"
"time"
)
//to keep stability of the ssh netWork
//this package for ssh pool
// run process:
// sshController --> sshPool --> sshConnection
const (
poolCount = 3
eachPoolConnectionCount = 3
)
type sshController struct {
ctx context.Context
sshTool *SSHTool
remoteHost string
totalPoolCount int64
eachPoolConnNums int64
currentPoolId int64
poolMutex *sync.Mutex
poolMap map[int64]*sshPool
exitPoolChan chan int64 //exit poolId
sshConnChan chan *sshConn //ssh all conn
}
func NewSShController(ctx context.Context, sshTool *SSHTool, remoteHost string) *sshController {
ctl := &sshController{
ctx: ctx,
sshTool: sshTool,
remoteHost: remoteHost,
totalPoolCount: poolCount,
eachPoolConnNums: eachPoolConnectionCount,
}
//init controller
ctl.init()
return ctl
}
func (ctl *sshController) Dispatcher() error {
//checkConnect
if err := ctl.checkConnect(); err != nil {
return err
}
//create poolMap
if err := ctl.createPools(); err != nil {
ctl.close()
return err
}
//loop
go ctl.loop()
return nil
}
func (ctl *sshController) GetNetConn() (netConn net.Conn, err error) {
ticker := time.NewTicker(time.Second * 3)
defer func() {
ticker.Stop()
}()
for {
select {
case conn := <-ctl.sshConnChan:
if conn.isActive() {
//pool make new conn signal
ctl.signalPoolNewConn(conn.poolId)
//return
return conn.netConn, nil
} else {
//reset pool conn
err := ctl.resetPoolConn(conn)
if err != nil {
log.Printf("[error][sshController.GetNetConn] resetPoolConn err. poolId:%d,connId:%d,err:%s", conn.poolId, conn.connId, err.Error())
}
}
//just continue get netConn
//timeout
case <-ticker.C:
return nil, fmt.Errorf("get net conn timeout")
//context canceled
case <-ctl.ctx.Done():
return nil, fmt.Errorf("context canceled")
}
}
return
}
func (ctl *sshController) init() error {
ctl.poolMutex = new(sync.Mutex)
ctl.poolMap = make(map[int64]*sshPool)
//exitPoolChan must has buffer, or it will deadlock
// when controller.close
// in the mean time pool.close send poolId to exitChan deadlock
ctl.exitPoolChan = make(chan int64, ctl.totalPoolCount)
ctl.sshConnChan = make(chan *sshConn, ctl.totalPoolCount*ctl.eachPoolConnNums)
return nil
}
func (ctl *sshController) signalPoolNewConn(poolId int64) {
ctl.poolMutex.Lock()
defer ctl.poolMutex.Unlock()
if pool, ok := ctl.poolMap[poolId]; ok {
<-pool.newConnChan
} else {
log.Printf("[error][sshController.signalPoolNewConn] pool(poolId:%d) not eixst", poolId)
}
}
func (ctl *sshController) checkConnect() error {
//open ssh tunnel
sshClient, err := ctl.sshTool.Connect()
if err != nil {
return fmt.Errorf("[sshController.checkConnect] sshTool.Connect err:%s", err.Error())
}
//check ssh remote host
testRemoteConn, err := sshClient.Dial("tcp", ctl.remoteHost)
if err != nil {
return fmt.Errorf("[sshController.checkConnect] ssh dial remoteHost(%s) err:%s", ctl.remoteHost, err.Error())
}
testRemoteConn.Close()
sshClient.Close()
return nil
}
func (ctl *sshController) createPools() error {
ctl.poolMutex.Lock()
defer ctl.poolMutex.Unlock()
var poolNum, failNum int64
maxFailNum := ctl.totalPoolCount * 4
for {
//if exceed total count
if poolNum >= ctl.totalPoolCount {
break
}
pool, err := ctl.newSSHPool()
if err != nil {
log.Printf("[error][sshController.createPools] newSSHPool error. err:%s", err.Error())
//increment failed num
failNum++
if failNum > maxFailNum {
log.Printf("[error][sshController.createPools] newSSHPool exceed maxFailNums(%d)", failNum)
return fmt.Errorf("create poolMap exceed max fail num:%d", failNum)
}
continue
}
ctl.poolMap[pool.poolId] = pool
poolNum++
}
return nil
}
func (ctl *sshController) loop() {
//pool dispatcher
ctl.poolMutex.Lock()
for poolId, pool := range ctl.poolMap {
go pool.Run()
log.Printf("[info][sshController.loop] pool(%d) Run", poolId)
}
ctl.poolMutex.Unlock()
//loop for
for {
select {
//wait ctx cancel
case <-ctl.ctx.Done():
ctl.close()
log.Printf("[info][sshController.loop] ctx done. controller close")
goto end
case poolId := <-ctl.exitPoolChan:
//reload poolId
ctl.reloadPool(poolId)
}
}
end:
}
func (ctl *sshController) reloadPool(oldPoolId int64) {
//ctx done
select {
case <-ctl.ctx.Done():
log.Printf("[info][sshController.reloadPool] context canceled. oldPoolId:%d", oldPoolId)
return
default:
}
ctl.poolMutex.Lock()
defer ctl.poolMutex.Unlock()
//close
if oldPool, ok := ctl.poolMap[oldPoolId]; ok {
if err := oldPool.close(); err != nil {
log.Printf("[error][sshController.reloadPool] pool close error.poolId:%d,err:%s", oldPool.poolId, err.Error())
}
}
//add new
var failNum int64
for {
newPool, err := ctl.newSSHPool()
if err != nil {
failNum++
if failNum > 5 {
log.Printf("[error][sshController.reloadPool] reload exceed max failNum(%d).oldPoolId:%d", failNum, oldPoolId)
break
}
continue
}
go newPool.Run()
//add new pool
ctl.poolMap[newPool.poolId] = newPool
log.Printf("[info][sshController.reloadPool] reload success.oldPoolId:%d,newPoolId:%d", oldPoolId, newPool.poolId)
break
}
delete(ctl.poolMap, oldPoolId)
}
func (ctl *sshController) newSSHPool() (*sshPool, error) {
sshClient, err := ctl.sshTool.Connect()
if err != nil {
log.Printf("[error][sshController.newSSHPool] ssh tool connect err:%s", err.Error())
return nil, fmt.Errorf("sshTool Connect err:%s", err.Error())
}
pool := &sshPool{
ctx: ctl.ctx,
sshClient: sshClient,
remoteHost: ctl.remoteHost,
maxConnNum: ctl.eachPoolConnNums,
poolId: atomic.AddInt64(&ctl.currentPoolId, 1),
exit: ctl.exitPoolChan,
sshConnChan: ctl.sshConnChan,
}
//init
pool.init()
return pool, nil
}
func (ctl *sshController) close() error {
ctl.poolMutex.Lock()
for poolId, pool := range ctl.poolMap {
pool.close()
log.Printf("[info][sshController.close] pool(%d) close", poolId)
}
ctl.poolMutex.Unlock()
//close ssh conn chan
for {
select {
case <-ctl.sshConnChan:
default:
goto end1sshConn
}
}
end1sshConn:
close(ctl.sshConnChan)
//close exit pool chan
for {
select {
case <-ctl.exitPoolChan:
default:
goto end2ExitPool
}
}
end2ExitPool:
close(ctl.exitPoolChan)
log.Printf("[info][sshController.close] close success")
return nil
}
func (ctl *sshController) resetPoolConn(conn *sshConn) error {
ctl.poolMutex.Lock()
defer ctl.poolMutex.Unlock()
if pool, ok := ctl.poolMap[conn.poolId]; ok {
pool.PutConn(conn)
} else {
return fmt.Errorf("conn not found pool(%d)", conn.poolId)
}
return nil
}
type sshPool struct {
ctx context.Context
remoteHost string
poolId int64
maxConnNum int64
sshClient *ssh.Client
currentConnId int64
//from sshController
exit chan int64
sshConnChan chan *sshConn
//self chan for async
closeChan chan struct{}
ackClose chan struct{}
newConnChan chan struct{}
returnedConnChan chan *sshConn
}
func (pool *sshPool) init() {
pool.closeChan = make(chan struct{})
pool.ackClose = make(chan struct{})
pool.newConnChan = make(chan struct{}, pool.maxConnNum)
pool.returnedConnChan = make(chan *sshConn, pool.maxConnNum)
}
func (pool *sshPool) PutConn(conn *sshConn) {
select {
case <-pool.ctx.Done():
log.Printf("[info][sshPool.PutConn] context canceled.")
return
case <-pool.closeChan:
log.Printf("[info][sshPool.PutConn] pool closed.")
return
default:
}
pool.returnedConnChan <- conn
}
func (pool *sshPool) Run() {
//monitor
go pool.monitor()
//watch
go pool.watchConn()
//loop
for {
//get signal
select {
case <-pool.closeChan:
log.Printf("[info][sshPool.Run][poolId:%d] pool closed", pool.poolId)
goto end
case <-pool.ctx.Done(): //context canceled
log.Printf("[info][sshPool.Run][poolId:%d] context canceled", pool.poolId)
goto end
case pool.newConnChan <- struct{}{}:
}
//new ssh
conn, err := pool.newSShConn()
if err != nil {
log.Printf("[error][sshPool.Run][poolId:%d] newSShConn err:%s", pool.poolId, err.Error())
//remove
<-pool.newConnChan
continue
}
if err := conn.ping(); err != nil {
log.Printf("[error][sshPool.Run][poolId:%d] sshConn ping err:%s", pool.poolId, err.Error())
//close
conn.close()
//remove
<-pool.newConnChan
continue
}
//put new conn
pool.sshConnChan <- conn
}
end:
//ack close
pool.ackClose <- struct{}{}
}
func (pool *sshPool) monitor() {
for {
err := pool.ping()
if err != nil {
log.Printf("[error][sshPool.monitor][poolId:%d] ping err:%s", pool.poolId, err.Error())
//pool exit
pool.exit <- pool.poolId
break
}
//context is canceled
if pool.isCanceled() {
log.Printf("[info][sshPool.monitor][poolId:%d] context canceled", pool.poolId)
break
}
//sleep 3s
sleepAwhile(3)
}
log.Printf("[info][sshPool.monitor][poolId:%d] monitor pool exit", pool.poolId)
//ack close
pool.ackClose <- struct{}{}
}
func (pool *sshPool) watchConn() {
for {
select {
case <-pool.ctx.Done():
log.Printf("[info][sshPool.watchConn][poolId:%d] context canceled", pool.poolId)
goto end
case <-pool.closeChan:
log.Printf("[info][sshPool.watchConn][poolId:%d] pool closed", pool.poolId)
goto end
case conn := <-pool.returnedConnChan:
if conn == nil {
continue
}
if conn.isExpired() { //conn expired
pool.expireConn(conn)
} else if conn.isNeedRefresh() { //conn need refresh
pool.refreshConn(conn)
} else if conn.isActive() { // conn active
pool.sshConnChan <- conn
} else {
log.Printf("[error][sshPool.watchConn][poolId:%d] conn condition not in (expire,needRefresh,active),connId:%d,creatTime:%d,updateTime:%d", pool.poolId, conn.connId, conn.createTime, conn.updateTime)
}
}
}
end:
//ack close
pool.ackClose <- struct{}{}
}
func (pool *sshPool) expireConn(conn *sshConn) {
conn.close()
<-pool.newConnChan
}
func (pool *sshPool) refreshConn(conn *sshConn) {
if err := conn.ping(); err != nil {
log.Printf("[error][sshPool.refreshConn][poolId:%d] conn ping error.connId:%d,err:%s", conn.poolId, conn.connId, err.Error())
conn.close()
<-pool.newConnChan
} else {
pool.sshConnChan <- conn
}
}
func (pool *sshPool) ping() error {
//space: []byte{'\x20'}
_, _, err := pool.sshClient.SendRequest("keepalive", true, []byte{'\x20'})
if err != nil {
log.Printf("[error][sshPool.monitor][poolId:%d] sshClient SendRequest err:%s", pool.poolId, err.Error())
return err
}
return nil
}
func (pool *sshPool) newSShConn() (*sshConn, error) {
netConn, err := pool.sshClient.Dial("tcp", pool.remoteHost)
if err != nil {
log.Printf("[error][sshPool.newSShConn] ssh dial remote host(%s) err:%s", pool.remoteHost, err.Error())
return nil, err
}
conn := sshConn{
netConn: netConn,
poolId: pool.poolId,
connId: atomic.AddInt64(&pool.currentConnId, 1),
createTime: time.Now().Unix(),
updateTime: time.Now().Unix(),
}
return &conn, nil
}
func (pool *sshPool) isCanceled() bool {
return pool.ctx.Err() != nil
}
func (pool *sshPool) close() error {
if err := pool.sshClient.Close(); err != nil {
log.Printf("[error][sshPool.close][poolId:%d] sshClient Close error.err:%s", pool.poolId, err.Error())
return fmt.Errorf("sshPool sshClient close err:%s", err.Error())
}
//close self chan
close(pool.closeChan)
//wait routine exit
<-pool.ackClose
<-pool.ackClose
<-pool.ackClose
//close ack
close(pool.ackClose)
//close new chan
for {
select {
case <-pool.newConnChan:
default:
goto end1New
}
}
end1New:
close(pool.newConnChan)
//close returned chan
for {
select {
case conn := <-pool.returnedConnChan:
if conn != nil {
conn.close()
}
default:
goto end2Returned
}
}
end2Returned:
close(pool.returnedConnChan)
pool.sshClient = nil
pool.sshConnChan = nil
pool.exit = nil
log.Printf("[info][sshPool.close][poolId:%d] pool close success", pool.poolId)
return nil
}
type sshConn struct {
netConn net.Conn
poolId int64
connId int64
createTime int64
updateTime int64
}
func (conn *sshConn) isExpired() bool {
return conn.createTime+30 < time.Now().Unix()
}
func (conn *sshConn) isNeedRefresh() bool {
//greater than 3 second
b1 := conn.updateTime+3 < time.Now().Unix()
// less than 30 second
b2 := conn.updateTime+30 > time.Now().Unix()
return b1 && b2
}
func (conn *sshConn) isActive() bool {
return conn.updateTime+3 > time.Now().Unix()
}
func (conn *sshConn) close() error {
return conn.netConn.Close()
}
func (conn *sshConn) ping() error {
//space: []byte{'\x20'}
conn.netConn.SetWriteDeadline(afterAwhile(2))
_, err := conn.netConn.Write([]byte{'\x20'})
if err != nil {
log.Printf("[error][sshConn.ping][poolId:%d][connId:%d] ssh write err:%s", conn.poolId, conn.connId, err.Error())
return fmt.Errorf("ping err:%s", err.Error())
}
conn.netConn.SetWriteDeadline(time.Time{})
//update time
conn.updateTime = time.Now().Unix()
return nil
}
//after seconds
func afterAwhile(duration time.Duration) time.Time {
return time.Now().Add(time.Second * duration)
}
//sleep seconds
func sleepAwhile(duration time.Duration) {
time.Sleep(time.Second * duration)
}