-
Notifications
You must be signed in to change notification settings - Fork 113
/
pool.go
652 lines (579 loc) · 18.2 KB
/
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
package radix
import (
"io"
"net"
"sync"
"sync/atomic"
"time"
errors "golang.org/x/xerrors"
"github.com/mediocregopher/radix/v3/resp"
"github.com/mediocregopher/radix/v3/trace"
)
// ErrPoolEmpty is used by Pools created using the PoolOnEmptyErrAfter option
var ErrPoolEmpty = errors.New("connection pool is empty")
var errPoolFull = errors.New("connection pool is full")
// ioErrConn is a Conn which tracks the last net.Error which was seen either
// during an Encode call or a Decode call
type ioErrConn struct {
Conn
// The most recent network error which occurred when either reading
// or writing. A critical network error is basically any non-application
// level error, e.g. a timeout, disconnect, etc... Close is automatically
// called on the client when it encounters a critical network error
lastIOErr error
}
func newIOErrConn(c Conn) *ioErrConn {
return &ioErrConn{Conn: c}
}
func (ioc *ioErrConn) Encode(m resp.Marshaler) error {
if ioc.lastIOErr != nil {
return ioc.lastIOErr
}
err := ioc.Conn.Encode(m)
if nerr, _ := err.(net.Error); nerr != nil {
ioc.lastIOErr = err
}
return err
}
func (ioc *ioErrConn) Decode(m resp.Unmarshaler) error {
if ioc.lastIOErr != nil {
return ioc.lastIOErr
}
err := ioc.Conn.Decode(m)
if nerr, _ := err.(net.Error); nerr != nil {
ioc.lastIOErr = err
} else if err != nil && !errors.As(err, new(resp.ErrDiscarded)) {
ioc.lastIOErr = err
}
return err
}
func (ioc *ioErrConn) Do(a Action) error {
return a.Run(ioc)
}
func (ioc *ioErrConn) Close() error {
ioc.lastIOErr = io.EOF
return ioc.Conn.Close()
}
////////////////////////////////////////////////////////////////////////////////
type poolOpts struct {
cf ConnFunc
pingInterval time.Duration
refillInterval time.Duration
overflowDrainInterval time.Duration
overflowSize int
onEmptyWait time.Duration
errOnEmpty error
pipelineConcurrency int
pipelineLimit int
pipelineWindow time.Duration
pt trace.PoolTrace
}
// PoolOpt is an optional behavior which can be applied to the NewPool function
// to effect a Pool's behavior
type PoolOpt func(*poolOpts)
// PoolConnFunc tells the Pool to use the given ConnFunc when creating new
// Conns to its redis instance. The ConnFunc can be used to set timeouts,
// perform AUTH, or even use custom Conn implementations.
func PoolConnFunc(cf ConnFunc) PoolOpt {
return func(po *poolOpts) {
po.cf = cf
}
}
// PoolPingInterval specifies the interval at which a ping event happens. On
// each ping event the Pool calls the PING redis command over one of it's
// available connections.
//
// Since connections are used in LIFO order, the ping interval * pool size is
// the duration of time it takes to ping every connection once when the pool is
// idle.
//
// A shorter interval means connections are pinged more frequently, but also
// means more traffic with the server.
func PoolPingInterval(d time.Duration) PoolOpt {
return func(po *poolOpts) {
po.pingInterval = d
}
}
// PoolRefillInterval specifies the interval at which a refill event happens. On
// each refill event the Pool checks to see if it is full, and if it's not a
// single connection is created and added to it.
func PoolRefillInterval(d time.Duration) PoolOpt {
return func(po *poolOpts) {
po.refillInterval = d
}
}
// PoolOnEmptyWait effects the Pool's behavior when there are no available
// connections in the Pool. The effect is to cause actions to block as long as
// it takes until a connection becomes available.
func PoolOnEmptyWait() PoolOpt {
return func(po *poolOpts) {
po.onEmptyWait = -1
}
}
// PoolOnEmptyCreateAfter effects the Pool's behavior when there are no
// available connections in the Pool. The effect is to cause actions to block
// until a connection becomes available or until the duration has passed. If the
// duration is passed a new connection is created and used.
//
// If wait is 0 then a new connection is created immediately upon an empty Pool.
func PoolOnEmptyCreateAfter(wait time.Duration) PoolOpt {
return func(po *poolOpts) {
po.onEmptyWait = wait
po.errOnEmpty = nil
}
}
// PoolOnEmptyErrAfter effects the Pool's behavior when there are no
// available connections in the Pool. The effect is to cause actions to block
// until a connection becomes available or until the duration has passed. If the
// duration is passed then ErrEmptyPool is returned.
//
// If wait is 0 then ErrEmptyPool is returned immediately upon an empty Pool.
func PoolOnEmptyErrAfter(wait time.Duration) PoolOpt {
return func(po *poolOpts) {
po.onEmptyWait = wait
po.errOnEmpty = ErrPoolEmpty
}
}
// PoolOnFullClose effects the Pool's behavior when it is full. The effect is to
// cause any connection which is being put back into a full pool to be closed
// and discarded.
func PoolOnFullClose() PoolOpt {
return func(po *poolOpts) {
po.overflowSize = 0
po.overflowDrainInterval = 0
}
}
// PoolOnFullBuffer effects the Pool's behavior when it is full. The effect is
// to give the pool an additional buffer for connections, called the overflow.
// If a connection is being put back into a full pool it will be put into the
// overflow. If the overflow is also full then the connection will be closed and
// discarded.
//
// drainInterval specifies the interval at which a drain event happens. On each
// drain event a connection will be removed from the overflow buffer (if any are
// present in it), closed, and discarded.
//
// If drainInterval is zero then drain events will never occur.
func PoolOnFullBuffer(size int, drainInterval time.Duration) PoolOpt {
return func(po *poolOpts) {
po.overflowSize = size
po.overflowDrainInterval = drainInterval
}
}
// PoolPipelineConcurrency sets the maximum number of pipelines that can be
// executed concurrently.
//
// If limit is greater than the pool size or less than 1, the limit will be
// set to the pool size.
func PoolPipelineConcurrency(limit int) PoolOpt {
return func(po *poolOpts) {
po.pipelineConcurrency = limit
}
}
// PoolPipelineWindow sets the duration after which internal pipelines will be
// flushed and the maximum number of commands that can be pipelined before
// flushing.
//
// If window is zero then implicit pipelining will be disabled.
// If limit is zero then no limit will be used and pipelines will only be limited
// by the specified time window.
func PoolPipelineWindow(window time.Duration, limit int) PoolOpt {
return func(po *poolOpts) {
po.pipelineLimit = limit
po.pipelineWindow = window
}
}
// PoolWithTrace tells the Pool to trace itself with the given PoolTrace
// Note that PoolTrace will block every point that you set to trace.
func PoolWithTrace(pt trace.PoolTrace) PoolOpt {
return func(po *poolOpts) {
po.pt = pt
}
}
////////////////////////////////////////////////////////////////////////////////
// Pool is a dynamic connection pool which implements the Client interface. It
// takes in a number of options which can effect its specific behavior; see the
// NewPool method.
//
// Pool is dynamic in that it can create more connections on-the-fly to handle
// increased load. The maximum number of extra connections (if any) can be
// configured, along with how long they are kept after load has returned to
// normal.
//
// Pool also takes advantage of implicit pipelining. If multiple commands are
// being performed simultaneously, then Pool will write them all to a single
// connection using a single system call, and read all their responses together
// using another single system call. Implicit pipelining significantly improves
// performance during high-concurrency usage, at the expense of slightly worse
// performance during low-concurrency usage. It can be disabled using
// PoolPipelineWindow(0, 0).
type Pool struct {
// Atomic fields must be at the beginning of the struct since they must be
// correctly aligned or else access may cause panics on 32-bit architectures
// See https://golang.org/pkg/sync/atomic/#pkg-note-BUG
totalConns int64 // atomic, must only be access using functions from sync/atomic
opts poolOpts
network, addr string
size int
l sync.RWMutex
// pool is read-protected by l, and should not be written to or read from
// when closed is true (closed is also protected by l)
pool chan *ioErrConn
closed bool
pipeliner *pipeliner
wg sync.WaitGroup
closeCh chan bool
initDone chan struct{} // used for tests
// Any errors encountered internally will be written to this channel. If
// nothing is reading the channel the errors will be dropped. The channel
// will be closed when Close is called.
ErrCh chan error
}
// NewPool creates a *Pool which will keep open at least the given number of
// connections to the redis instance at the given address.
//
// NewPool takes in a number of options which can overwrite its default
// behavior. The default options NewPool uses are:
//
// PoolConnFunc(DefaultConnFunc)
// PoolOnEmptyCreateAfter(1 * time.Second)
// PoolRefillInterval(1 * time.Second)
// PoolOnFullBuffer((size / 3)+1, 1 * time.Second)
// PoolPingInterval(5 * time.Second / (size+1))
// PoolPipelineConcurrency(size)
// PoolPipelineWindow(150 * time.Microsecond, 0)
//
// The recommended size of the pool depends on the number of concurrent
// goroutines that will use the pool and whether implicit pipelining is
// enabled or not.
//
// As a general rule, when implicit pipelining is enabled (the default)
// the size of the pool can be kept low without problems to reduce resource
// and file descriptor usage.
//
func NewPool(network, addr string, size int, opts ...PoolOpt) (*Pool, error) {
p := &Pool{
network: network,
addr: addr,
size: size,
closeCh: make(chan bool),
initDone: make(chan struct{}),
ErrCh: make(chan error, 1),
}
defaultPoolOpts := []PoolOpt{
PoolConnFunc(DefaultConnFunc),
PoolOnEmptyCreateAfter(1 * time.Second),
PoolRefillInterval(1 * time.Second),
PoolOnFullBuffer((size/3)+1, 1*time.Second),
PoolPingInterval(5 * time.Second / time.Duration(size+1)),
PoolPipelineConcurrency(size),
// NOTE if 150us is changed the benchmarks need to be updated too
PoolPipelineWindow(150*time.Microsecond, 0),
}
for _, opt := range append(defaultPoolOpts, opts...) {
// the other args to NewPool used to be a ConnFunc, which someone might
// have left as nil, in which case this now gives a weird panic. Just
// handle it
if opt != nil {
opt(&(p.opts))
}
}
totalSize := size + p.opts.overflowSize
p.pool = make(chan *ioErrConn, totalSize)
// make one Conn synchronously to ensure there's actually a redis instance
// present. The rest will be created asynchronously.
ioc, err := p.newConn(trace.PoolConnCreatedReasonInitialization)
if err != nil {
return nil, err
}
p.put(ioc)
p.wg.Add(1)
go func() {
startTime := time.Now()
defer p.wg.Done()
for i := 0; i < size-1; i++ {
ioc, err := p.newConn(trace.PoolConnCreatedReasonInitialization)
if err != nil {
p.err(err)
// if there was an error connecting to the instance than it
// might need a little breathing room, redis can sometimes get
// sad if too many connections are created simultaneously.
time.Sleep(100 * time.Millisecond)
continue
} else if !p.put(ioc) {
// if the connection wasn't put in it could be for two reasons:
// - the Pool has already started being used and is full.
// - Close was called.
// in any case, bail
break
}
}
close(p.initDone)
p.traceInitCompleted(time.Since(startTime))
}()
// needs to be created before starting any background goroutines to avoid
// races on p.pipeliner access
if p.opts.pipelineWindow > 0 {
if p.opts.pipelineConcurrency < 1 || p.opts.pipelineConcurrency > size {
p.opts.pipelineConcurrency = size
}
p.pipeliner = newPipeliner(
p,
p.opts.pipelineConcurrency,
p.opts.pipelineLimit,
p.opts.pipelineWindow,
)
}
if p.opts.pingInterval > 0 && size > 0 {
p.atIntervalDo(p.opts.pingInterval, func() { p.Do(Cmd(nil, "PING")) })
}
if p.opts.refillInterval > 0 && size > 0 {
p.atIntervalDo(p.opts.refillInterval, p.doRefill)
}
if p.opts.overflowSize > 0 && p.opts.overflowDrainInterval > 0 {
p.atIntervalDo(p.opts.overflowDrainInterval, p.doOverflowDrain)
}
return p, nil
}
func (p *Pool) traceInitCompleted(elapsedTime time.Duration) {
if p.opts.pt.InitCompleted != nil {
p.opts.pt.InitCompleted(trace.PoolInitCompleted{
PoolCommon: p.traceCommon(),
AvailCount: len(p.pool),
ElapsedTime: elapsedTime,
})
}
}
func (p *Pool) err(err error) {
select {
case p.ErrCh <- err:
default:
}
}
func (p *Pool) traceCommon() trace.PoolCommon {
return trace.PoolCommon{
Network: p.network, Addr: p.addr,
PoolSize: p.size, BufferSize: p.opts.overflowSize,
}
}
func (p *Pool) traceConnCreated(connectTime time.Duration, reason trace.PoolConnCreatedReason, err error) {
if p.opts.pt.ConnCreated != nil {
p.opts.pt.ConnCreated(trace.PoolConnCreated{
PoolCommon: p.traceCommon(),
Reason: reason,
ConnectTime: connectTime,
Err: err,
})
}
}
func (p *Pool) traceConnClosed(reason trace.PoolConnClosedReason) {
if p.opts.pt.ConnClosed != nil {
p.opts.pt.ConnClosed(trace.PoolConnClosed{
PoolCommon: p.traceCommon(),
AvailCount: len(p.pool),
Reason: reason,
})
}
}
func (p *Pool) newConn(reason trace.PoolConnCreatedReason) (*ioErrConn, error) {
start := time.Now()
c, err := p.opts.cf(p.network, p.addr)
elapsed := time.Since(start)
p.traceConnCreated(elapsed, reason, err)
if err != nil {
return nil, err
}
ioc := newIOErrConn(c)
atomic.AddInt64(&p.totalConns, 1)
return ioc, nil
}
func (p *Pool) atIntervalDo(d time.Duration, do func()) {
p.wg.Add(1)
go func() {
defer p.wg.Done()
t := time.NewTicker(d)
defer t.Stop()
for {
select {
case <-t.C:
do()
case <-p.closeCh:
return
}
}
}()
}
func (p *Pool) doRefill() {
if atomic.LoadInt64(&p.totalConns) >= int64(p.size) {
return
}
ioc, err := p.newConn(trace.PoolConnCreatedReasonRefill)
if err == nil {
p.put(ioc)
} else if err != errPoolFull {
p.err(err)
}
}
func (p *Pool) doOverflowDrain() {
// the other do* processes inherently handle this case, this one needs to do
// it manually
p.l.RLock()
if p.closed || len(p.pool) <= p.size {
p.l.RUnlock()
return
}
// pop a connection off and close it, if there's any to pop off
var ioc *ioErrConn
select {
case ioc = <-p.pool:
default:
// pool is empty, nothing to drain
}
p.l.RUnlock()
if ioc == nil {
return
}
ioc.Close()
p.traceConnClosed(trace.PoolConnClosedReasonBufferDrain)
atomic.AddInt64(&p.totalConns, -1)
}
func (p *Pool) getExisting() (*ioErrConn, error) {
// Fast-path if the pool is not empty. Return error if pool has been closed.
select {
case ioc, ok := <-p.pool:
if !ok {
return nil, errClientClosed
}
return ioc, nil
default:
}
if p.opts.onEmptyWait == 0 {
// If we should not wait we return without allocating a timer.
return nil, p.opts.errOnEmpty
}
// only set when we have a timeout, since a nil channel always blocks which
// is what we want
var tc <-chan time.Time
if p.opts.onEmptyWait > 0 {
t := getTimer(p.opts.onEmptyWait)
defer putTimer(t)
tc = t.C
}
select {
case ioc, ok := <-p.pool:
if !ok {
return nil, errClientClosed
}
return ioc, nil
case <-tc:
return nil, p.opts.errOnEmpty
}
}
func (p *Pool) get() (*ioErrConn, error) {
ioc, err := p.getExisting()
if err != nil {
return nil, err
} else if ioc != nil {
return ioc, nil
}
return p.newConn(trace.PoolConnCreatedReasonPoolEmpty)
}
// returns true if the connection was put back, false if it was closed and
// discarded.
func (p *Pool) put(ioc *ioErrConn) bool {
p.l.RLock()
if ioc.lastIOErr == nil && !p.closed {
select {
case p.pool <- ioc:
p.l.RUnlock()
return true
default:
}
}
p.l.RUnlock()
// the pool might close here, but that's fine, because all that's happening
// at this point is that the connection is being closed
ioc.Close()
p.traceConnClosed(trace.PoolConnClosedReasonPoolFull)
atomic.AddInt64(&p.totalConns, -1)
return false
}
// Do implements the Do method of the Client interface by retrieving a Conn out
// of the pool, calling Run on the given Action with it, and returning the Conn
// to the pool.
//
// If the given Action is a CmdAction, it will be pipelined with other concurrent
// calls to Do, which can improve the performance and resource usage of the Redis
// server, but will increase the latency for some of the Actions. To avoid the
// implicit pipelining you can either set PoolPipelineWindow(0, 0) when creating the
// Pool or use WithConn. Pipelines created manually (via Pipeline) are also excluded
// from this and will be executed as if using WithConn.
//
// Due to a limitation in the implementation, custom CmdAction implementations
// are currently not automatically pipelined.
func (p *Pool) Do(a Action) error {
startTime := time.Now()
if p.pipeliner != nil && p.pipeliner.CanDo(a) {
err := p.pipeliner.Do(a)
p.traceDoCompleted(time.Since(startTime), err)
return err
}
c, err := p.get()
if err != nil {
return err
}
err = c.Do(a)
p.put(c)
p.traceDoCompleted(time.Since(startTime), err)
return err
}
func (p *Pool) traceDoCompleted(elapsedTime time.Duration, err error) {
if p.opts.pt.DoCompleted != nil {
p.opts.pt.DoCompleted(trace.PoolDoCompleted{
PoolCommon: p.traceCommon(),
AvailCount: len(p.pool),
ElapsedTime: elapsedTime,
Err: err,
})
}
}
// NumAvailConns returns the number of connections currently available in the
// pool, as well as in the overflow buffer if that option is enabled.
func (p *Pool) NumAvailConns() int {
return len(p.pool)
}
// Close implements the Close method of the Client
func (p *Pool) Close() error {
p.l.Lock()
if p.closed {
p.l.Unlock()
return errClientClosed
}
p.closed = true
close(p.closeCh)
// at this point get and put won't work anymore, so it's safe to empty and
// close the pool channel
emptyLoop:
for {
select {
case ioc := <-p.pool:
ioc.Close()
atomic.AddInt64(&p.totalConns, -1)
p.traceConnClosed(trace.PoolConnClosedReasonPoolClosed)
default:
close(p.pool)
break emptyLoop
}
}
p.l.Unlock()
if p.pipeliner != nil {
if err := p.pipeliner.Close(); err != nil {
return err
}
}
// by now the pool's go-routines should have bailed, wait to make sure they
// do
p.wg.Wait()
close(p.ErrCh)
return nil
}