-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool.go
174 lines (147 loc) · 2.96 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
package connexus
import (
"container/heap"
"errors"
"net"
"sort"
"sync"
"time"
)
var (
ErrClosed = errors.New("pool is closed")
)
const MaxConnectCount = 20000
const MaxIdleConnectCount = 25000
type Pool interface {
// Get return a new item from the pool. Closing the item puts it back to the pool
Get() (net.Conn, error)
// Close close the pool and release all resources
Close()
// Len returns the number of items of the pool
Len() int
}
type PoolConfig struct {
Cap int
MaxIdleCap int
Factory func() (net.Conn, error)
}
type connexPool struct {
mu sync.Mutex
freeConn *PriorityQueue
cap int
maxIdleCap int
cleanerCh chan struct{}
count int
factory func() (net.Conn, error)
}
func NewConnexPool(cfg PoolConfig) (Pool, error) {
if cfg.Cap > cfg.MaxIdleCap {
return nil, errors.New("Cap can not more than MaxIdleCap")
}
var poolCap int
var idleConnCap int
if cfg.Cap > MaxConnectCount {
poolCap = MaxConnectCount
} else {
poolCap = cfg.Cap
}
if cfg.MaxIdleCap > MaxIdleConnectCount {
idleConnCap = MaxIdleConnectCount
} else {
idleConnCap = cfg.Cap
}
cp := &connexPool{
cap: poolCap,
cleanerCh: make(chan struct{}, 1),
factory: cfg.Factory,
maxIdleCap: idleConnCap,
}
pq := make(PriorityQueue, 0, cfg.Cap)
heap.Init(&pq)
cp.freeConn = &pq
for i := 0; i < poolCap; i++ {
conn, err := cfg.Factory()
if err != nil {
continue
}
cp.put(cp.wrapConn(conn).(*Connex))
cp.count++
}
go cp.inducer()
return cp, nil
}
func (cp *connexPool) Get() (net.Conn, error) {
cp.mu.Lock()
defer cp.mu.Unlock()
if cp.freeConn == nil {
return nil, ErrClosed
}
if cp.freeConn.Len() > 0 {
return heap.Pop(cp.freeConn).(*Connex), nil
}
if cp.count < cp.maxIdleCap {
conn, err := cp.factory()
cp.count++
if err != nil {
return nil, err
}
return cp.wrapConn(conn), nil
}
return nil, errors.New("can not allocate connect")
}
func (cp *connexPool) Close() {
cp.mu.Lock()
defer cp.mu.Unlock()
if cp.freeConn == nil {
return
}
cp.cleanerCh <- struct{}{}
cp.factory = nil
for cp.freeConn.Len() > 0 {
c := heap.Pop(cp.freeConn).(*Connex)
c.cp = nil
c.Conn.Close()
}
cp.freeConn = nil
}
func (cp *connexPool) put(conn *Connex) error {
cp.mu.Lock()
defer cp.mu.Unlock()
if cp.freeConn == nil {
return ErrClosed
}
if cp.freeConn.Len() >= cp.cap {
return errors.New("pool have been filled")
}
heap.Push(cp.freeConn, conn)
return nil
}
func (cp *connexPool) Len() int {
cp.mu.Lock()
defer cp.mu.Unlock()
if cp.freeConn == nil {
return 0
}
return cp.freeConn.Len()
}
func (cp *connexPool) inducer() {
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case <-ticker.C:
cp.mu.Lock()
if cp.freeConn != nil {
sort.Sort(cp.freeConn)
}
cp.mu.Unlock()
case <-cp.cleanerCh:
return
}
}
}
func (cp *connexPool) wrapConn(conn net.Conn) net.Conn {
p := &Connex{cp: cp, updatedTime: time.Now()}
p.Conn = conn
return p
}