-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool.go
207 lines (187 loc) · 4.31 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
package pool
import (
"errors"
"golang.org/x/crypto/ssh"
"sync"
"time"
)
type Item struct {
Object *ssh.Client
Expiration int64
}
// Expired Returns true if the item has expired.
func (item Item) Expired() bool {
return time.Now().Unix() > item.Expiration
}
type Pool struct {
items map[string]chan Item
defaultExpiration time.Duration
cleanupInterval time.Duration
maxConn int
eachKeyMaxConn int
currentConn int
cleanerStop chan struct{}
mu sync.Mutex
}
// NewPool creates a new pool.
// defaultExpiration: The default expiration time.
// cleanupInterval: How often the pool is checked for expired items.
// maxConn: The maximum number of connections that can be held by the pool.
// eachKeyMaxConn: The maximum number of connections that can be held by the pool for each key.
func NewPool(defaultExpiration, cleanupInterval time.Duration, maxConn, eachKeyMaxConn int) *Pool {
if maxConn == 0 {
maxConn = 1000
}
if eachKeyMaxConn == 0 {
eachKeyMaxConn = 3
}
if defaultExpiration <= 0 {
defaultExpiration = 10 * time.Minute
}
if cleanupInterval <= 0 {
cleanupInterval = 10 * time.Minute
}
items := make(map[string]chan Item, 8)
pool := &Pool{
items: items,
defaultExpiration: defaultExpiration,
cleanupInterval: cleanupInterval,
maxConn: maxConn,
eachKeyMaxConn: eachKeyMaxConn,
cleanerStop: make(chan struct{}),
}
go pool.cleaner()
return pool
}
func (p *Pool) Len() int {
return p.currentConn
}
// Put adds an ssh Client to the pool
// key: suggested key is user + host + port
func (p *Pool) Put(key string, c *ssh.Client) (err error) {
p.mu.Lock()
defer p.mu.Unlock()
if p.currentConn >= p.maxConn {
return errors.New("pool is full")
}
if _, ok := p.items[key]; !ok {
p.items[key] = make(chan Item, p.eachKeyMaxConn)
}
if len(p.items[key]) >= p.eachKeyMaxConn {
return errors.New("key is full")
}
p.items[key] <- Item{
Object: c,
Expiration: time.Now().Add(p.defaultExpiration).Unix(),
}
p.currentConn++
return nil
}
func (p *Pool) get(key string) (*Item, bool) {
p.mu.Lock()
defer p.mu.Unlock()
if _, ok := p.items[key]; !ok {
return nil, false
}
itemNum := len(p.items[key])
for i := 0; i < itemNum; i++ {
item := <-p.items[key]
p.currentConn--
if len(p.items[key]) == 0 {
delete(p.items, key)
}
if item.Expired() {
_ = item.Object.Close()
continue
}
return &item, true
}
return nil, false
}
// Get return ssh client from the pool by the key, and the ssh client will be removed from the pool.
func (p *Pool) Get(key string) (*ssh.Client, bool) {
for {
item, ok := p.get(key)
if !ok {
return nil, false
}
// if the ssh client put into the pool less than 60 seconds, skip the keepalive test.
if time.Now().Add(p.defaultExpiration).Unix()-item.Expiration < 60 {
return item.Object, true
}
err := KeepAlive(item.Object)
if err != nil {
_ = item.Object.Close()
continue
}
return item.Object, true
}
}
func (p *Pool) Delete(key string) {
p.mu.Lock()
defer p.mu.Unlock()
if _, ok := p.items[key]; !ok {
return
}
itemNum := len(p.items[key])
for i := 0; i < itemNum; i++ {
item := <-p.items[key]
p.currentConn--
_ = item.Object.Close()
continue
}
return
}
// GetWithNew return ssh client from the pool, if there is no client in the pool, it will create a new one.
func (p *Pool) GetWithNew(key, user, host string, port int, opts ...ClientCfgOption) (c *ssh.Client, err error) {
c, ok := p.Get(key)
if ok {
return c, nil
}
c, err = NewSSHClient(user, host, port, opts...)
if err != nil {
return nil, err
}
return c, nil
}
func (p *Pool) clean() {
p.mu.Lock()
defer p.mu.Unlock()
for key, itemCh := range p.items {
for i := 0; i < len(itemCh); i++ {
item := <-itemCh
if item.Expired() {
_ = item.Object.Close()
continue
}
itemCh <- item
}
if len(itemCh) == 0 {
delete(p.items, key)
}
}
}
func (p *Pool) cleaner() {
ticker := time.NewTicker(p.cleanupInterval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
p.clean()
case <-p.cleanerStop:
return
}
}
}
func (p *Pool) Close() {
p.mu.Lock()
defer p.mu.Unlock()
close(p.cleanerStop)
for _, itemCh := range p.items {
for i := 0; i < len(itemCh); i++ {
item := <-itemCh
_ = item.Object.Close()
}
}
p.items = nil
}