-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconn_pool.go
More file actions
222 lines (201 loc) · 4.46 KB
/
Copy pathconn_pool.go
File metadata and controls
222 lines (201 loc) · 4.46 KB
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
package accelerator
import (
"context"
"errors"
"net"
"sync"
"sync/atomic"
"time"
)
var (
errEmptyConnPool = errors.New("empty connection pool")
errConnPoolClosed = errors.New("connection pool is closed")
)
// connPool is used to send frame packet for lower RTT.
// Server side use Push for delegate data to sender in
// the conn pool for prevent block when one user block.
type connPool struct {
logger *logger
size atomic.Value
timeout time.Duration
conns map[*net.Conn]bool
connsRWM sync.RWMutex
frameCh chan []byte
closed int32
ctx context.Context
cancel context.CancelFunc
wg sync.WaitGroup
}
func newConnPool(logger *logger, size int, timeout time.Duration, server bool) *connPool {
pool := connPool{
logger: logger,
timeout: timeout,
conns: make(map[*net.Conn]bool, size),
}
pool.ctx, pool.cancel = context.WithCancel(context.Background())
pool.SetSize(size)
if !server {
return &pool
}
pool.frameCh = make(chan []byte, 128*size)
for i := 0; i < size; i++ {
pool.wg.Add(1)
go pool.sendLoop()
}
return &pool
}
// SetSize is used to set connection pool size.
func (pool *connPool) SetSize(size int) {
pool.size.Store(size)
}
// GetSize is used to get connection pool size.
func (pool *connPool) GetSize() int {
return pool.size.Load().(int)
}
// IsEmpty is used to check connection pool is empty.
func (pool *connPool) IsEmpty() bool {
if pool.isClosed() {
return true
}
pool.connsRWM.RLock()
defer pool.connsRWM.RUnlock()
return len(pool.conns) < 1
}
// IsFull is used to check connection pool is full.
func (pool *connPool) IsFull() bool {
if pool.isClosed() {
return true
}
pool.connsRWM.RLock()
defer pool.connsRWM.RUnlock()
return pool.isFull()
}
func (pool *connPool) isFull() bool {
return len(pool.conns) >= pool.GetSize()
}
// AddConn is used to add new connection to the pool.
func (pool *connPool) AddConn(conn *net.Conn) bool {
pool.connsRWM.Lock()
defer pool.connsRWM.Unlock()
if pool.isFull() || pool.isClosed() {
return false
}
pool.conns[conn] = false
return true
}
// DeleteConn is used to delete connection in the pool.
func (pool *connPool) DeleteConn(conn *net.Conn) {
pool.connsRWM.Lock()
defer pool.connsRWM.Unlock()
if pool.isClosed() {
return
}
delete(pool.conns, conn)
}
// Push is used to push data to connection pool and wait sender
// to send data, if send queue is full, data will be dropped.
func (pool *connPool) Push(b []byte) {
data := make([]byte, len(b))
copy(data, b)
select {
case pool.frameCh <- data:
default:
}
}
func (pool *connPool) sendLoop() {
defer pool.wg.Done()
defer func() {
if r := recover(); r != nil {
pool.logger.Fatal("connPool.sendLoop", r)
// restart frame sender
time.Sleep(time.Second)
pool.wg.Add(1)
go pool.sendLoop()
}
}()
var b []byte
for {
select {
case b = <-pool.frameCh:
_, _ = pool.Write(b)
case <-pool.ctx.Done():
return
}
}
}
// Write is used to select one connection for write data.
func (pool *connPool) Write(b []byte) (int, error) {
var (
conn net.Conn
n int
err error
)
for i := 0; i < 10; i++ {
conn, err = pool.getConn()
if err != nil {
if err != errEmptyConnPool {
return 0, err
}
// wait some time for add new connection
select {
case <-time.After(10 * time.Millisecond):
case <-pool.ctx.Done():
return 0, errConnPoolClosed
}
continue
}
err = conn.SetWriteDeadline(time.Now().Add(pool.timeout))
if err != nil {
continue
}
n, err = conn.Write(b)
if err == nil {
return n, nil
}
_ = conn.Close()
}
return n, err
}
func (pool *connPool) getConn() (net.Conn, error) {
pool.connsRWM.Lock()
defer pool.connsRWM.Unlock()
if len(pool.conns) < 1 {
return nil, errEmptyConnPool
}
for {
if pool.isClosed() {
return nil, errConnPoolClosed
}
// select unused connection
for conn, used := range pool.conns {
if !used {
pool.conns[conn] = true
return *conn, nil
}
}
// reset all used flags
for conn := range pool.conns {
pool.conns[conn] = false
}
}
}
func (pool *connPool) isClosed() bool {
return atomic.LoadInt32(&pool.closed) != 0
}
// Close is used to close all connections.
func (pool *connPool) Close() error {
atomic.StoreInt32(&pool.closed, 1)
pool.cancel()
pool.wg.Wait()
var err error
pool.connsRWM.Lock()
defer pool.connsRWM.Unlock()
for conn := range pool.conns {
e := (*conn).Close()
if e != nil && !errors.Is(e, net.ErrClosed) && err == nil {
err = e
}
delete(pool.conns, conn)
}
return err
}