-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconn_pool.go
199 lines (168 loc) · 4.3 KB
/
conn_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
// common connection pool to connect to another server
//caller asure to return the connection to the pool
package connpool
import (
"container/list"
"sync"
"time"
"errors"
"fmt"
)
var NowFunc = time.Now // for testing
type ConnPool struct {
Connect func() (interface{}, int, error) //connect func,return instance,id,error
DisConnect func(c interface{}, id int) //disconnect func
//must init param
// Maximum number of connections allocated by the pool at a given time.
// When zero, there is no limit on the number of connections in the pool.
MaxActiveNum int
//Reserved idle connections
ReservedIdleNum int
// Close connections after remaining idle for this duration. If the value
// is zero, then idle connections are not closed. Applications should set
// the timeout to a value less than the server's timeout.
IdleTimeout time.Duration
// How many seconds wait for when the pool is at the MaxActiveNum limit
//0 forever,-1 no wait
WaitTime int64
mu sync.RWMutex
cond *TMOCond
closed bool
idlePool list.List
//internal param
activeNum int //current inuse num
waitNum int //wait num
}
type Conn struct {
t time.Time //time duration
Err error // whether the conn is error
Inst interface{} //conn instance
ID int //the id of the conn,given by the connect func
}
// new connection pool
func NewConnectionPool(maxActiveNum int, revIdleNum int, idleTimeout time.Duration, waitTime int64, connectFunc func() (interface{}, int, error), disConnectFunc func(c interface{}, id int)) *ConnPool {
return &ConnPool{
MaxActiveNum: maxActiveNum,
ReservedIdleNum: revIdleNum,
IdleTimeout: idleTimeout,
WaitTime: waitTime,
Connect: connectFunc,
DisConnect: disConnectFunc,
}
}
//pop an connection from pool
func (p *ConnPool) Pop() (*Conn, error) {
var c *Conn
tryed := false
p.mu.Lock()
//for loop to close idle timeout conn and close them
if timeout := p.IdleTimeout; timeout > 0 {
for i, n := p.ReservedIdleNum, p.idlePool.Len(); i < n; i++ {
e := p.idlePool.Back()
if e == nil {
break
}
c := e.Value.(*Conn)
if c.t.Add(timeout).After(NowFunc()) {
break
}
p.idlePool.Remove(e)
p.mu.Unlock()
go p.DisConnect(c.Inst, c.ID)
p.mu.Lock()
}
}
for {
//Get idle connection.
if p.idlePool.Len() > 0 {
e := p.idlePool.Front()
c = e.Value.(*Conn)
p.idlePool.Remove(e)
// mark as in use
p.activeNum += 1
p.mu.Unlock()
return c, nil
}
// Check for pool closed before dialing a new connection.
if p.closed {
p.mu.Unlock()
return nil, errors.New("pool closed")
}
if p.MaxActiveNum == 0 || p.activeNum < p.MaxActiveNum {
p.activeNum += 1
p.mu.Unlock()
//new connection
Inst, id, e := p.Connect()
if e != nil {
p.mu.Lock()
p.activeNum -= 1
p.mu.Unlock()
return nil, fmt.Errorf("connection pool:%s", e.Error())
}
// init struct
c = &Conn{Inst: Inst, ID: id}
return c, nil
}
//no wait
if p.WaitTime < 0 {
p.mu.Unlock()
return nil, nil
}
if tryed {
return nil, errors.New("pop wait timeout")
}
if p.cond == nil {
p.cond = NewTMOCond(&p.mu)
}
p.waitNum += 1
if p.WaitTime > 0 {
p.cond.WaitOrTimeout(time.Second * time.Duration(p.WaitTime))
tryed = true
}else {
p.cond.Wait()
}
p.waitNum -= 1
}
}
//push an connection to pool.(you shoud not op c after push the conn to pool)
func (p *ConnPool) Push(c *Conn) error {
if c == nil {
return errors.New("connection pool:[Push] c == nil")
}
// if the conn is err,drop it
if c.Err != nil {
p.mu.Lock()
p.activeNum -= 1
p.mu.Unlock()
p.DisConnect(c.Inst, c.ID)
//return fmt.Errorf("connection pool:drop error connection,id:%d,err:%s", c.ID, c.Err.Error())
return nil
}
c.t = NowFunc()
p.mu.Lock()
p.idlePool.PushFront(c)
p.activeNum -= 1
if p.cond != nil {
p.cond.Signal()
}
p.mu.Unlock()
return nil
}
func (p *ConnPool) GetActiveNum() int {
p.mu.RLock()
an := p.activeNum
p.mu.RUnlock()
return an
}
func (p *ConnPool) GetIdleNum() int {
p.mu.RLock()
in := p.idlePool.Len()
p.mu.RUnlock()
return in
}
func (p *ConnPool) GetWaitNum() int {
p.mu.RLock()
waitNum := p.waitNum
p.mu.RUnlock()
return waitNum
}