-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool.go
139 lines (125 loc) · 2.54 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
package pool
import (
"container/list"
"log"
"sync"
"time"
)
type Option struct {
addr string
size int
readTimeout time.Duration
dialTimeout time.Duration
keepAlive time.Duration
}
type Pool struct {
*Option
idle *list.List // idle doubly linked list
actives int // total connection count
mtx *sync.Mutex
cond *sync.Cond
}
// NewPool initialization of the pool queue
func NewPool(opt *Option) (p *Pool, err error) {
idle := list.New()
var conn *Conn
for i := 0; i < opt.size; i++ {
conn, err = NewConn(opt)
if err == nil {
idle.PushBack(conn)
}
// whether close all idle conn when one of err occurs?
}
mutx := new(sync.Mutex)
cond := sync.NewCond(mutx)
p = &Pool{
opt,
idle,
idle.Len(),
mutx,
cond,
}
return
}
/*
Get() :
Is the free list in idle queue:
- no
- Whether the number of connections has reached the upper limit
- yes, unlocked, blocked waiting to wake up
- no, Create a connection
- pop from head of the queue
*/
func (p *Pool) Get() (c *Conn, err error) {
p.mtx.Lock()
defer p.mtx.Unlock()
// If the current activity over the limit number, block and wait
for p.idle.Len() == 0 && p.actives >= p.size {
log.Print("idle size full, blocking...")
// cancel possessing and release the mutex lock,
// it will wake up automatically when the for condition does not hold
p.cond.Wait()
}
if p.idle.Len() > 0 {
c = p.idle.Remove(p.idle.Front()).(*Conn)
} else {
c, err = NewConn(p.Option)
if err == nil {
p.actives++
}
}
return
}
/*
Put()
- Is the connection alive?
- no, close it
- yes, return link to end of line
- Update the number of occupied connections, wake up the waiting side
*/
func (p *Pool) Put(c *Conn, err error) {
p.mtx.Lock()
defer p.mtx.Unlock()
if err != nil {
if c != nil {
c.Close()
}
} else {
p.idle.PushBack(c)
}
p.actives--
p.cond.Signal()
}
func (p *Pool) Close() (err error) {
for n := p.idle.Front(); n == nil; n = n.Next() {
n.Value.(*Conn).Close()
}
return
}
// Reset the idle list
func (p *Pool) Reset() {
newIdle := list.New()
var conn *Conn
var err error
for i := 0; i < p.Option.size; i++ {
conn, err = NewConn(p.Option)
if err != nil {
for e := newIdle.Front(); e != nil; e = e.Next() {
e.Value.(*Conn).Close()
}
return
}
newIdle.PushBack(conn)
}
var oldIdle *list.List
p.mtx.Lock()
oldIdle = p.idle
p.idle = newIdle
p.actives = newIdle.Len()
p.mtx.Unlock()
if oldIdle != nil {
for e := oldIdle.Front(); e != nil; e = e.Next() {
e.Value.(*Conn).Close()
}
}
}