-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheasypool.go
194 lines (166 loc) · 4.69 KB
/
easypool.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
package easypool
import (
"sync"
"time"
"log"
"errors"
)
var (
TimeoutError = errors.New("get connection timeout")
OperationAtClosedPoolError = errors.New("pool has been shutdown")
)
type EasyPool struct {
config PoolConfig
Coons chan Closable // channel for connections
Mux sync.RWMutex
Closed bool
closingChan chan struct{}
current int // connections count currently in the pool
busy bool // current status.true, put back can be putinto the pool directly. false, put will check if connection count is greater than core.yes, will discard the connection
times chan int64 // times channel, summary the time of every Get, to calculate Get() for every second
marktime int64 //current time,time.Unix()
markCount int // Get() executed times at markTime
markTimes int // continuous times over the threadhold or behind the threadhold
}
func InitEasyPool(config PoolConfig) *EasyPool {
easyPool := new(EasyPool)
easyPool.config = config
easyPool.busy = true
easyPool.Coons = make(chan Closable, config.Max)
easyPool.Closed = false
easyPool.closingChan = make(chan struct{}, 1)
easyPool.busy = true
easyPool.times = make(chan int64, 1000)
easyPool.marktime = time.Now().Unix()
easyPool.markCount = 0
easyPool.markTimes = 0
easyPool.current = 0
go easyPool.switchStatus()
return easyPool
}
func (easyPool *EasyPool) switchStatus() {
for ; ; {
if (!easyPool.Closed) {
cur := <-easyPool.times
if easyPool.marktime == cur {
easyPool.markCount++
// when busy is false and markCount increases quickly, when markCount is greater than 2 times of threadhold, busy will change to true, will not wait the trigger condition of threadhold and threadholdTimes
if (!easyPool.busy && easyPool.markCount > 2*easyPool.config.Threadhold) {
easyPool.busy = true
}
} else {
easyPool.marktime = cur
lastMarkCount := easyPool.markCount
easyPool.markCount = 1
// a new second comes, marktime updated. need to check if fulfill the condition of threadhold and threadholdTimes to trigger busy change
if (easyPool.busy) {
if (lastMarkCount < easyPool.config.Threadhold) {
easyPool.markTimes++
if (easyPool.markTimes >= easyPool.config.ThreadholdTimes) {
easyPool.busy = false
easyPool.markTimes = 0;
}
} else {
easyPool.markTimes = 0
}
} else {
if (lastMarkCount >= easyPool.config.Threadhold) {
easyPool.markTimes++
if (easyPool.markTimes >= easyPool.config.ThreadholdTimes) {
easyPool.busy = true
easyPool.markTimes = 0;
}
} else {
easyPool.markTimes = 0
}
}
}
} else {
log.Fatal("switchStatus check exit for pool has been closed")
return
}
}
}
func (easyPool *EasyPool) pinPoint(cur int64) {
easyPool.times <- cur
}
func (easyPool *EasyPool) Put(conn Closable) error {
if (easyPool.Closed) {
log.Fatal("easyPool has been Closed")
return OperationAtClosedPoolError
}
easyPool.Mux.Lock()
defer easyPool.Mux.Unlock()
if (easyPool.busy) {
easyPool.Coons <- conn
} else {
if len(easyPool.Coons) < easyPool.config.Core {
easyPool.Coons <- conn
} else {
easyPool.current--
conn.Close()
}
}
return nil
}
func (easyPool *EasyPool) Get() (Closable, error) {
if (easyPool.Closed) {
log.Fatal("easyPool has been Closed")
return nil, OperationAtClosedPoolError
}
go easyPool.pinPoint(time.Now().Unix())
select {
case conn := <-easyPool.Coons:
return conn, nil
default:
if (easyPool.current < easyPool.config.Max) {
easyPool.Mux.RLock()
defer easyPool.Mux.RUnlock()
conn, err := easyPool.config.Factory()
if (err != nil) {
log.Fatal(err)
return nil, err
}
easyPool.current++
return conn, nil
} else {
for {
select {
case <-time.After(5 * time.Second):
//log.Fatal("can't get connection %s after 5S", easyPool.config.Name)
return nil, TimeoutError
case conn := <-easyPool.Coons:
return conn, nil
case <-easyPool.closingChan:
easyPool.Mux.RLock()
defer easyPool.Mux.RUnlock()
easyPool.current--
if easyPool.current >= 0 {
easyPool.closingChan <- struct{}{}
}
return nil, OperationAtClosedPoolError
}
}
}
}
}
func (easyPool *EasyPool) Close() {
if (easyPool.Closed) {
return
}
easyPool.Mux.Lock()
easyPool.closingChan <- struct{}{}
close(easyPool.Coons)
defer easyPool.Mux.Unlock()
easyPool.Closed = true
for closable := range easyPool.Coons {
closable.Close()
}
}
func (easyPool *EasyPool) Size() (int, error) {
if (easyPool.Closed) {
log.Fatal("easyPool has been Closed")
return 0, OperationAtClosedPoolError
}
return easyPool.current, nil
}