1
0
mirror of https://github.com/redis/go-redis.git synced 2025-07-31 05:04:23 +03:00

internal/pool: exit conn pool fast (#1155)

* internal/pool: exit conn pool reaper fast
This commit is contained in:
Ou Changkun
2019-09-09 11:50:28 +03:00
committed by Vladimir Mihailenco
parent 3e1f1aba0e
commit cb2d1c89e6

View File

@ -78,7 +78,8 @@ type ConnPool struct {
stats Stats stats Stats
_closed uint32 // atomic _closed uint32 // atomic
closedCh chan struct{}
} }
var _ Pooler = (*ConnPool)(nil) var _ Pooler = (*ConnPool)(nil)
@ -90,6 +91,7 @@ func NewConnPool(opt *Options) *ConnPool {
queue: make(chan struct{}, opt.PoolSize), queue: make(chan struct{}, opt.PoolSize),
conns: make([]*Conn, 0, opt.PoolSize), conns: make([]*Conn, 0, opt.PoolSize),
idleConns: make([]*Conn, 0, opt.PoolSize), idleConns: make([]*Conn, 0, opt.PoolSize),
closedCh: make(chan struct{}),
} }
p.checkMinIdleConns() p.checkMinIdleConns()
@ -416,6 +418,7 @@ func (p *ConnPool) Close() error {
if !atomic.CompareAndSwapUint32(&p._closed, 0, 1) { if !atomic.CompareAndSwapUint32(&p._closed, 0, 1) {
return ErrClosed return ErrClosed
} }
close(p.closedCh)
var firstErr error var firstErr error
p.connsMu.Lock() p.connsMu.Lock()
@ -437,14 +440,22 @@ func (p *ConnPool) reaper(frequency time.Duration) {
ticker := time.NewTicker(frequency) ticker := time.NewTicker(frequency)
defer ticker.Stop() defer ticker.Stop()
for range ticker.C { for {
if p.closed() { select {
break case <-ticker.C:
} // It is possible that ticker and closedCh arrive together,
_, err := p.ReapStaleConns() // and select pseudo-randomly pick ticker case, we double
if err != nil { // check here to prevent being executed after closed.
internal.Logger.Printf("ReapStaleConns failed: %s", err) if p.closed() {
continue return
}
_, err := p.ReapStaleConns()
if err != nil {
internal.Logger.Printf("ReapStaleConns failed: %s", err)
continue
}
case <-p.closedCh:
return
} }
} }
} }