forked from shaovie/goev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect_pool.go
219 lines (190 loc) · 5.02 KB
/
connect_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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package goev
import (
"container/list"
"errors"
"runtime"
"sync"
"sync/atomic"
"time"
"github.com/shaovie/goev/netfd"
)
// ConnectPoolHandler is the interface that wraps the basic Conn handle method
type ConnectPoolHandler interface {
EvHandler
setPool(cp *ConnectPool)
GetPool() *ConnectPool
Closed()
}
// ConnectPoolItem is the base object
type ConnectPoolItem struct {
IOHandle
cp *ConnectPool
}
func (cph *ConnectPoolItem) setPool(cp *ConnectPool) {
cph.cp = cp
}
// GetPool can retrieve the current conn object bound to which ConnectPool
func (cph *ConnectPoolItem) GetPool() *ConnectPool {
return cph.cp
}
// Closed when a conn is detected as closed, it needs to notify the ConnectPool to perform resource recycling.
func (cph *ConnectPoolItem) Closed() {
cph.cp.closed()
}
// ConnectPool provides a reusable connection pool that can dynamically scale and manage network connections
type ConnectPool struct {
minIdleNum int
addNumOnceTime int
maxLiveNum int
connectTimeout int64
addr string
connector *Connector
ticker *time.Ticker
conns *list.List
connsMtx sync.Mutex
toNewNum atomic.Int32
liveNum atomic.Int32
newConnectPoolHandlerFunc func() ConnectPoolHandler
emptySig chan struct{}
newConnChan chan newConnInPool
}
type newConnInPool struct {
fd int
ch ConnectPoolHandler
}
// NewConnectPool return an instance
//
// The addr format 192.168.0.1:8080
func NewConnectPool(c *Connector, addr string, minIdleNum, addNumOnceTime, maxLiveNum int,
connectTimeout, keepNumTicker int64, // millisecond
newConnectPoolHandlerFunc func() ConnectPoolHandler) (*ConnectPool, error) {
if minIdleNum < 1 || minIdleNum >= maxLiveNum || maxLiveNum < addNumOnceTime {
panic("NewConnectPool min/add/max invalid")
}
r := c.GetReactor()
if r == nil {
return nil, errors.New("connector invalid")
}
cp := &ConnectPool{
minIdleNum: minIdleNum,
addNumOnceTime: addNumOnceTime,
maxLiveNum: maxLiveNum,
connectTimeout: connectTimeout,
addr: addr,
connector: c,
conns: list.New(),
newConnectPoolHandlerFunc: newConnectPoolHandlerFunc,
ticker: time.NewTicker(time.Millisecond * time.Duration(keepNumTicker)),
newConnChan: make(chan newConnInPool, runtime.NumCPU()*2),
emptySig: make(chan struct{}),
}
go cp.keepNumTiming()
go cp.handleNewConn()
return cp, nil
}
// Acquire returns a usable connection handler, and if none is available, it returns nil
func (cp *ConnectPool) Acquire() ConnectPoolHandler {
cp.connsMtx.Lock()
item := cp.conns.Front()
if item == nil {
cp.connsMtx.Unlock()
cp.emptySig <- struct{}{}
return nil
}
cp.conns.Remove(item)
cp.connsMtx.Unlock()
return item.Value.(ConnectPoolHandler)
}
// Release accepts a reusable connection
func (cp *ConnectPool) Release(ch ConnectPoolHandler) {
if ch.GetPool() != cp {
panic("ConnectPool.Release ch doesn't belong to this pool")
}
if ch == nil {
panic("ConnectPool.Release ch is nil")
}
cp.connsMtx.Lock()
cp.conns.PushBack(ch)
cp.connsMtx.Unlock()
}
// IdleNum returns the number of idle connections
func (cp *ConnectPool) IdleNum() int {
cp.connsMtx.Lock()
defer cp.connsMtx.Unlock()
return cp.conns.Len()
}
// LiveNum returns the number of active connections
func (cp *ConnectPool) LiveNum() int {
return int(cp.liveNum.Load())
}
func (cp *ConnectPool) keepNumTiming() {
for {
select {
case <-cp.emptySig:
cp.keepNum()
case <-cp.ticker.C:
cp.keepNum()
}
}
}
func (cp *ConnectPool) keepNum() {
// 1. keep min size
idleNum := cp.IdleNum()
toNewNum := 0
if idleNum < cp.minIdleNum {
toNewNum = cp.addNumOnceTime
liveNum := cp.LiveNum()
if liveNum == 0 {
toNewNum = cp.minIdleNum
} else if toNewNum+liveNum > cp.maxLiveNum {
toNewNum = cp.maxLiveNum - liveNum
}
}
if toNewNum < 1 {
return
}
if !cp.toNewNum.CompareAndSwap(0, int32(toNewNum)) {
return
}
for i := 0; i < toNewNum; i++ {
if err := cp.connector.Connect(cp.addr, &connectPoolConn{cp: cp}, cp.connectTimeout); err != nil {
cp.toNewNum.Add(-1)
}
}
}
func (cp *ConnectPool) handleNewConn() {
for {
select {
case conn := <-cp.newConnChan:
cp.onNewConn(conn.fd, conn.ch)
}
}
}
func (cp *ConnectPool) onNewConn(fd int, ch ConnectPoolHandler) {
if ch.OnOpen(fd) == false {
return
}
cp.liveNum.Add(1)
cp.Release(ch)
}
func (cp *ConnectPool) closed() {
cp.liveNum.Add(-1)
}
type connectPoolConn struct {
IOHandle
cp *ConnectPool
}
func (cpc *connectPoolConn) OnOpen(fd int) bool {
cpc.cp.toNewNum.Add(-1)
netfd.SetKeepAlive(fd, 60, 40, 3)
connHandler := cpc.cp.newConnectPoolHandlerFunc()
connHandler.setReactor(cpc.GetReactor())
connHandler.setPool(cpc.cp)
cpc.cp.newConnChan <- newConnInPool{fd: fd, ch: connHandler}
return false
}
func (cpc *connectPoolConn) OnConnectFail(err error) {
cpc.cp.toNewNum.Add(-1)
}
func (cpc *connectPoolConn) OnClose() {
}