-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpool.go
201 lines (187 loc) · 4.39 KB
/
gpool.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
package gpool
import (
"container/list"
"errors"
"sync"
"time"
log "github.com/sirupsen/logrus"
)
var (
// ErrCanNotGetItem return when get item failed
ErrCanNotGetItem = errors.New("Unable GET Item")
// ErrTypeConvert return when Convert item to require type
ErrTypeConvert = errors.New("Class Cast Failed")
// ErrItemInitialFailed return when Initial item failed
ErrItemInitialFailed = errors.New("Iem Initial Failed")
// ErrPoolExtendFailed return when Initial pool failed
ErrPoolExtendFailed = errors.New("Pool Extend Failed")
// ErrHasBeenShotdown return when do something after pool has been shutdown
ErrHasBeenShotdown = errors.New("Pool has been shutdown")
)
//Pool pool class
type Pool struct {
Config Config
items *list.List
newItemFunc Creator
cond *sync.Cond
shutdown bool
}
//DefaultPool create a pool with default config
func DefaultPool(creater Creator) *Pool {
return &Pool{
Config: DefaultConfig(),
items: list.New(),
newItemFunc: creater,
cond: sync.NewCond(&sync.Mutex{}),
shutdown: false,
}
}
//Initial initial pool
func (pool *Pool) Initial() {
log.WithField(
"config", pool.Config,
).Debug("START Initial Pool")
pool.cond.L.Lock()
defer pool.cond.L.Unlock()
// Initial pool item
go pool.extend(pool.Config.InitialPoolSize)
pool.cond.Wait()
// start cyclicity check goroutine
go pool.startCheck()
log.Debug("Done Initial Pool")
}
//Extend push item into pool
func (pool *Pool) extend(count int) {
log.WithField(
"count", count,
).Debug("START | Extend Pool")
pool.cond.L.Lock()
defer pool.cond.Signal()
defer pool.cond.L.Unlock()
if pool.shutdown {
log.Debug(ErrHasBeenShotdown)
return
}
for i := 0; i < count; i++ {
if pool.items.Len() >= pool.Config.MaxPoolSize {
break
}
var item = pool.newItemFunc()
err := item.Initial(pool.Config.Params)
if err != nil {
log.Error(ErrItemInitialFailed)
continue
}
pool.items.PushBack(item)
}
log.Debug("DONE | Extend Pool")
}
//StartCheck start check avaiable goroutine
func (pool *Pool) startCheck() {
t := time.NewTicker(time.Duration(pool.Config.TestDuration) * time.Millisecond)
for {
<-t.C
pool.checkAvaiable()
}
}
//CheckAvaiable check item avaiable
func (pool *Pool) checkAvaiable() {
pool.cond.L.Lock()
defer pool.cond.L.Unlock()
log.WithField(
"pool Size", pool.items.Len(),
).Debug("START | CheckAvaiable ")
if pool.shutdown {
return
}
for i := pool.items.Front(); i != nil; {
item, Ok := i.Value.(Item)
if !Ok {
log.Error(ErrTypeConvert)
}
err := item.Check(pool.Config.Params)
if err == nil {
i = i.Next()
continue
} else {
log.WithError(err).Error("CheckAvaiable ERROR ")
n := i.Next()
pool.items.Remove(i)
i = n
}
}
log.Debug("DONE | CheckAvaiable")
}
//GetOne get a pool item
func (pool *Pool) GetOne() (Item, error) {
pool.cond.L.Lock()
defer pool.cond.L.Unlock()
if pool.shutdown {
return nil, ErrHasBeenShotdown
}
retry := 0
var i *list.Element
for {
i = pool.items.Front()
if i != nil {
break
}
if retry <= pool.Config.AcquireRetryAttempts {
retry++
go pool.extend(pool.Config.AcquireIncrement)
pool.cond.Wait()
continue
} else {
return nil, ErrCanNotGetItem
}
}
pool.items.Remove(i)
if pool.items.Len() < pool.Config.MinPoolSize {
go pool.extend(pool.Config.AcquireIncrement)
}
item, ok := i.Value.(Item)
if !ok {
return nil, ErrTypeConvert
}
if !pool.Config.TestOnGetItem {
return item, nil
}
if err := item.Check(pool.Config.Params); err != nil {
return nil, err
}
return item, nil
}
//BackOne give back a pool item
func (pool *Pool) BackOne(item Item) {
pool.cond.L.Lock()
defer pool.cond.L.Unlock()
if pool.items.Len() >= pool.Config.MaxPoolSize {
err := item.Destory(pool.Config.Params)
if err != nil {
log.WithError(err).Error("Item Destory ERROR While BackOne")
}
return
}
pool.items.PushBack(item)
}
//Shutdown shutdown pool
func (pool *Pool) Shutdown(wg *sync.WaitGroup) {
log.Debug("START | Shutdown Pool")
defer wg.Done()
pool.cond.L.Lock()
pool.shutdown = true
defer pool.cond.L.Unlock()
for i := pool.items.Front(); i != nil; i = pool.items.Front() {
pool.items.Remove(i)
item, ok := i.Value.(Item)
if !ok {
log.Error(ErrTypeConvert)
continue
}
err := item.Destory(pool.Config.Params)
if err != nil {
log.WithError(err).Error("Item Destory ERROR While Shutdown ")
}
}
log.Debug("DONE | Shutdown Pool")
}