-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathpool.go
More file actions
299 lines (243 loc) · 6.49 KB
/
pool.go
File metadata and controls
299 lines (243 loc) · 6.49 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
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 storm
import (
"context"
"errors"
deluge "github.com/gdm85/go-libdeluge"
"go.uber.org/zap"
"sync"
"time"
)
type DelugeProvider func() deluge.DelugeClient
type poolReq struct {
// If the context has been cancelled then no connection is returned
ctx context.Context
// The replied client may be nil if the connection could not be established
reply chan<- deluge.DelugeClient
}
func (req *poolReq) Send(conn deluge.DelugeClient) bool {
select {
case <-req.ctx.Done():
return false
case req.reply <- conn:
return true
}
}
type idleConnection struct {
idle time.Time
conn deluge.DelugeClient
}
type Timer interface {
Ch() <-chan time.Time
Stop() bool
}
type timeTimer time.Timer
func (t *timeTimer) Ch() <-chan time.Time {
return (*time.Timer)(t).C
}
func (t *timeTimer) Stop() bool {
return (*time.Timer)(t).Stop()
}
type nullTimer struct{}
func (nullTimer) Ch() <-chan time.Time {
return nil
}
func (nullTimer) Stop() bool {
return true
}
func NewConnectionPool(log *zap.Logger, maxConnections int, idleConnectionTime time.Duration, provider DelugeProvider) *ConnectionPool {
pool := &ConnectionPool{
Log: log,
MaxConnections: maxConnections,
IdleConnectionTime: idleConnectionTime,
Provider: provider,
get: make(chan *poolReq),
put: make(chan deluge.DelugeClient),
close: make(chan struct{}),
alive: new(sync.Mutex),
idle: nullTimer{},
}
go pool.worker()
return pool
}
type ConnectionPool struct {
Log *zap.Logger
MaxConnections int
IdleConnectionTime time.Duration
Provider DelugeProvider
get chan *poolReq
put chan deluge.DelugeClient
close chan struct{}
alive *sync.Mutex
waitConn []*poolReq
inFlight int
pool []*idleConnection
idle Timer
}
func (pool *ConnectionPool) nextIdle() {
// Go through the other connections.
// Check that they are not expired, if not set the new idle to the first valid connection
for {
if len(pool.pool) == 0 {
// Otherwise, there are no more connections to make idle
pool.idle = nullTimer{}
return
}
conn := pool.pool[0]
expires := conn.idle.Sub(time.Now())
// Next connection is now idle. Delete it.
if expires < 0 {
err := conn.conn.Close()
if err != nil {
pool.Log.Error("Failed to closed idle connection", zap.Error(err))
}
pool.pool = pool.pool[1:]
continue
}
// Valid connection that is not idle
t := time.NewTimer(expires)
pool.idle = (*timeTimer)(t)
return
}
}
func (pool *ConnectionPool) idleExpired() {
var conn *idleConnection
conn, pool.pool = pool.pool[0], pool.pool[1:]
err := conn.conn.Close()
if err != nil {
pool.Log.Error("Failed to closed idle connection", zap.Error(err))
}
pool.idle.Stop()
pool.nextIdle()
}
func (pool *ConnectionPool) putConn(conn deluge.DelugeClient) {
// Check if anyone is waiting for a connection
for len(pool.waitConn) > 0 {
w := pool.waitConn[0]
pool.waitConn[0] = nil
pool.waitConn = pool.waitConn[1:]
select {
case <-w.ctx.Done(): // waiter's connection has been cancelled
case w.reply <- conn: // waiter has received connection
return
}
}
idle := &idleConnection{idle: time.Now().Add(pool.IdleConnectionTime), conn: conn}
// There are no existing connections in the pool.
// Set the new pool timer
if len(pool.pool) == 0 {
pool.idle.Stop()
t := time.NewTimer(pool.IdleConnectionTime)
pool.idle = (*timeTimer)(t)
}
pool.pool = append(pool.pool, idle)
pool.inFlight--
}
func (pool *ConnectionPool) closeConns() {
pool.idle.Stop()
pool.idle = nullTimer{}
// Close any waiters
for len(pool.waitConn) > 0 {
var w *poolReq
w, pool.waitConn = pool.waitConn[0], pool.waitConn[1:]
close(w.reply)
}
// Close all connections within the pool
for len(pool.pool) > 0 {
var c *idleConnection
c, pool.pool = pool.pool[0], pool.pool[1:]
_ = c.conn.Close()
}
}
func (pool *ConnectionPool) getConn(req *poolReq) {
// There are connections that can be sent straight away
if len(pool.pool) > 0 {
c := pool.pool[0]
if req.Send(c.conn) {
// Connection successfully sent
pool.pool = pool.pool[1:]
pool.inFlight++
pool.idle.Stop()
pool.nextIdle()
}
return
}
// There are more connections in-flight.
// Add the request to the list of waiting requests.
if pool.inFlight >= pool.MaxConnections {
pool.waitConn = append(pool.waitConn, req)
return
}
// A new connection can be established
conn := pool.Provider()
err := conn.Connect()
if err != nil {
pool.Log.Error("Failed to establish Deluge RPC connection", zap.Error(err))
conn = nil
}
ok := req.Send(conn)
// The connection was nil so we don't care what the send response was
if conn == nil {
return
}
pool.inFlight++
// Connection successfully sent
if ok {
return
}
// Connection was established but could not be sent to the caller
// Put the established connection into the pool
pool.putConn(conn)
}
func (pool *ConnectionPool) worker() {
pool.alive.Lock()
defer pool.alive.Unlock()
for {
select {
case <-pool.idle.Ch(): // The first connection is now idle
pool.idleExpired()
case req := <-pool.get:
pool.getConn(req)
case conn := <-pool.put: // A connection has been put back
pool.putConn(conn)
case <-pool.close:
pool.closeConns()
return
}
}
}
// Get gets a connected connection from the pool.
// If there are no available connections in the pool then one is created and connected to.
// If there already too many active connections, Get will block until a connection is available
// or the given context is cancelled.
func (pool *ConnectionPool) Get(ctx context.Context) (deluge.DelugeClient, error) {
// TODO someone needs to close this
replyCh := make(chan deluge.DelugeClient)
pool.get <- &poolReq{ctx: ctx, reply: replyCh}
select {
case <-pool.close:
return nil, errors.New("The Deluge RPC connection pool has been closed")
case <-ctx.Done():
return nil, ctx.Err()
case conn := <-replyCh:
if conn == nil {
return nil, errors.New("A connection to the Deluge RPC daemon could not be established by the connection pool")
}
return conn, nil
}
}
// Put puts a connection back to the pool.
func (pool *ConnectionPool) Put(conn deluge.DelugeClient) {
select {
case <-pool.close:
// Pool has been closed before the connection can be put back
_ = conn.Close()
case pool.put <- conn:
}
}
func (pool *ConnectionPool) Close() {
close(pool.close)
// Achieve a lock on the pool alive mutex.
// When a lock is achieved the pool worker daemon has been closed.
// Keep the alive mutex locked.
pool.alive.Lock()
}