This repository has been archived by the owner on Jul 9, 2018. It is now read-only.
/
conn_pool.go
299 lines (278 loc) · 7.14 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package proxy
import (
"errors"
"fmt"
"github.com/absolute8511/proxymodule/util"
"io"
"net"
"sync/atomic"
"time"
)
const (
RSP_BUF_SIZE = 1024
BUF_MAX_NUM = 200
MAX_CONN_NUM = 100
SHRINK_THRESHOLD = 10
SHRINK_NUM = 2
CONN_TIMEOUT = 3
)
type ProxyConnData struct {
conn *net.TCPConn
rspBuf chan *util.FixedBuffer
closing int32
holding chan struct{}
closeNotify chan struct{}
}
func NewProxyConnData(c *net.TCPConn) *ProxyConnData {
return &ProxyConnData{
conn: c,
rspBuf: make(chan *util.FixedBuffer, 10),
closing: 0,
holding: make(chan struct{}, 1),
closeNotify: make(chan struct{}),
}
}
func (self *ProxyConnData) Close() {
if atomic.CompareAndSwapInt32(&self.closing, 0, 1) {
close(self.closeNotify)
}
self.conn.CloseRead()
}
type TCPConnPool struct {
pid string
address string
lastUsed time.Time
connChan chan *ProxyConnData
used int32
bufPool *util.FixedBufferPool
useRspBuf bool
}
// if use response buffer, the pool will manager the read from remote and
// the client should read from the buffer. Otherwise, the client can read
// from remote directly.
func NewTCPConnPool(userspbuf bool, pid, address string, p *util.FixedBufferPool) *TCPConnPool {
ret := &TCPConnPool{
pid: pid,
address: address,
lastUsed: time.Now(),
connChan: make(chan *ProxyConnData, MAX_CONN_NUM),
used: 0,
useRspBuf: userspbuf,
}
ret.bufPool = p
if ret.bufPool == nil {
ret.bufPool = util.NewFixedBufferPool(RSP_BUF_SIZE, BUF_MAX_NUM, false)
}
return ret
}
func (self *TCPConnPool) readRspFromRemote(remote *ProxyConnData) {
//readbuf := make([]byte, RSP_BUF_SIZE)
bytesNum := 0
for {
data := self.bufPool.GetBuffer()
n, err := remote.conn.Read(data.GetRawBuffer())
if err != nil {
if err != io.EOF {
glog.Errorf("!!! read error from remote: %v", err)
}
self.bufPool.PutBuffer(data)
break
}
if n == 0 {
glog.Infof("read 0 from remote without error.")
self.bufPool.PutBuffer(data)
continue
}
bytesNum += n
data.UseBuffer(int32(n))
select {
case remote.rspBuf <- data:
case <-remote.closeNotify:
self.bufPool.PutBuffer(data)
break
}
}
if atomic.CompareAndSwapInt32(&remote.closing, 0, 1) {
close(remote.closeNotify)
}
glog.Debugf("remote proxy connection %v-%v finished, total : %v bytes.",
self.address, remote.conn.LocalAddr().String(), bytesNum)
// we should free the buffer from pool
select {
case remote.holding <- struct{}{}:
finished := false
for !finished {
select {
case b := <-remote.rspBuf:
self.bufPool.PutBuffer(b)
default:
if len(remote.rspBuf) == 0 {
finished = true
}
}
}
<-remote.holding
}
close(remote.rspBuf)
// the local may left data to send to remote,
// so only close read to let the data send last time.
remote.conn.CloseRead()
}
func (self *TCPConnPool) waitConn(t time.Duration) (bool, *ProxyConnData, error) {
isNewConn := false
wt := time.NewTimer(t)
for {
select {
case c := <-self.connChan:
if atomic.LoadInt32(&c.closing) == 1 {
glog.Debugf("ignore closing connection while acquire. %v : %v\n", self.address, c.conn.LocalAddr().String())
continue
}
if len(c.rspBuf) > 0 {
glog.Errorf("found a not empty rspbuf: %v, %v, %v", len(c.rspBuf), self.address, self.pid)
c.Close()
continue
}
c.holding <- struct{}{}
atomic.AddInt32(&self.used, 1)
return isNewConn, c, nil
case <-wt.C:
wt.Stop()
return isNewConn, nil, errors.New("time out")
}
}
}
func (self *TCPConnPool) AcquireConn() (bool, *ProxyConnData, error) {
isNewConn := false
for {
select {
case c := <-self.connChan:
if atomic.LoadInt32(&c.closing) == 1 {
glog.Debugf("ignore closing connection while acquire. %v : %v\n", self.address, c.conn.LocalAddr().String())
continue
}
if len(c.rspBuf) > 0 {
glog.Errorf("found a not empty rspbuf: %v, %v, %v", len(c.rspBuf), self.address, self.pid)
c.Close()
continue
//return isNewConn, c, errors.New("remote state not ok.")
}
c.holding <- struct{}{}
atomic.AddInt32(&self.used, 1)
return isNewConn, c, nil
default:
// new conn
if atomic.LoadInt32(&self.used) > MAX_CONN_NUM*2 {
glog.Warningf("Too many connections :%v, %v, used: %v", self.address, self.pid, self.used)
isNewConn, c, err := self.waitConn(time.Second)
if err == nil && c != nil {
return isNewConn, c, err
}
}
proxyconn := NewProxyConnData(nil)
// new conn
c, err := net.DialTimeout("tcp", self.address, time.Duration(10)*time.Second)
if err != nil {
glog.Infof("Connect to %v failed: %v\n", self.address, err.Error())
return isNewConn, nil, err
}
isNewConn = true
proxyconn.conn = c.(*net.TCPConn)
glog.Debugf("new connection to remote %v.\n", self.address)
if self.useRspBuf {
go self.readRspFromRemote(proxyconn)
}
proxyconn.holding <- struct{}{}
atomic.AddInt32(&self.used, 1)
return isNewConn, proxyconn, nil
}
}
}
func (self *TCPConnPool) ReleaseConn(p *ProxyConnData) {
//glog.V(3).Infof("== connection begin release: %v, %v, %v.", self.address, self.pid, id)
if len(p.rspBuf) > 0 {
glog.Warningf("connection rspBuf left data : %v. %v", len(p.rspBuf), self.address)
atomic.StoreInt32(&p.closing, 1)
}
select {
case <-p.holding:
// release hold
default:
// already released
return
}
atomic.AddInt32(&self.used, -1)
self.lastUsed = time.Now()
if atomic.LoadInt32(&p.closing) == 1 {
p.Close()
return
}
select {
case self.connChan <- p:
// release to pool
glog.Debugf("== connection ready for reuse: %v, %v.", self.address, self.pid)
default:
// pool is full, just discard connection
glog.Debugf("== connection discard : %v, %v.", self.address, self.pid)
p.Close()
}
}
func (self *TCPConnPool) DestroyRemoteConn(proxyconn *ProxyConnData) {
atomic.StoreInt32(&proxyconn.closing, 1)
select {
case <-proxyconn.holding:
// release hold
default:
// already released
return
}
atomic.AddInt32(&self.used, -1)
self.lastUsed = time.Now()
if len(proxyconn.rspBuf) > 0 {
glog.Infof("connection rspBuf left data : %v.", len(proxyconn.rspBuf))
}
glog.Debugf("====== connection destroyed: %v, %v.", self.address, self.pid)
proxyconn.Close()
}
func (self *TCPConnPool) IsUnusedSince(t time.Time) bool {
if self.lastUsed.After(t) {
return false
}
if atomic.LoadInt32(&self.used) == 0 {
return true
}
return false
}
func (self *TCPConnPool) ShrinkPool() {
if len(self.connChan) < SHRINK_THRESHOLD+SHRINK_NUM {
return
}
for i := 0; i < SHRINK_NUM; i++ {
select {
case c := <-self.connChan:
c.Close()
default:
return
}
}
}
func (self *TCPConnPool) CleanPool() {
for {
select {
case c := <-self.connChan:
c.Close()
default:
glog.Infof("pool cleaned: %v, %v", self.address, self.pid)
return
}
}
// maybe wait for all outside used remote to close.
}
func (self *TCPConnPool) IsEmpty() bool {
return len(self.connChan) == 0 && atomic.LoadInt32(&self.used) <= 0
}
func (self *TCPConnPool) StatusString() string {
statusStr := fmt.Sprintf(" %v : used - %v, free - %v, lastUsed:%v\n", self.address, self.used,
len(self.connChan), self.lastUsed)
return statusStr
}