diff --git a/mux.go b/mux.go index 4645a18f..a9b06066 100644 --- a/mux.go +++ b/mux.go @@ -100,8 +100,8 @@ func newMux(dst string, option *ClientOption, init, dead wire, wireFn wireFn, wi m.wire[i].Store(init) } - m.dpool = newPool(option.BlockingPoolSize, dead, option.IdleConnTTL, option.BlockingPoolMinSize, wireFn) - m.spool = newPool(option.BlockingPoolSize, dead, option.IdleConnTTL, option.BlockingPoolMinSize, wireNoBgFn) + m.dpool = newPool(option.BlockingPoolSize, dead, option.BlockingPoolCleanup, option.BlockingPoolMinSize, wireFn) + m.spool = newPool(option.BlockingPoolSize, dead, option.BlockingPoolCleanup, option.BlockingPoolMinSize, wireNoBgFn) return m } diff --git a/pool.go b/pool.go index a698e2f8..167462d8 100644 --- a/pool.go +++ b/pool.go @@ -11,29 +11,29 @@ func newPool(cap int, dead wire, idleConnTTL time.Duration, minSize int, makeFn } return &pool{ - size: 0, - minSize: minSize, - cap: cap, - dead: dead, - make: makeFn, - list: make([]wire, 0, 4), - cond: sync.NewCond(&sync.Mutex{}), - idleConnTTL: idleConnTTL, + size: 0, + minSize: minSize, + cap: cap, + dead: dead, + make: makeFn, + list: make([]wire, 0, 4), + cond: sync.NewCond(&sync.Mutex{}), + cleanup: idleConnTTL, } } type pool struct { - dead wire - cond *sync.Cond - make func() wire - list []wire - size int - minSize int - cap int - down bool - idleConnTTL time.Duration - timer *time.Timer - timerIsActive bool + dead wire + cond *sync.Cond + timer *time.Timer + make func() wire + list []wire + cleanup time.Duration + size int + minSize int + cap int + down bool + timerOn bool } func (p *pool) Acquire() (v wire) { @@ -49,6 +49,7 @@ func (p *pool) Acquire() (v wire) { } else { i := len(p.list) - 1 v = p.list[i] + p.list[i] = nil p.list = p.list[:i] } p.cond.L.Unlock() @@ -80,15 +81,15 @@ func (p *pool) Close() { } func (p *pool) startTimerIfNeeded() { - if p.idleConnTTL == 0 || p.timerIsActive || len(p.list) <= p.minSize { + if p.cleanup == 0 || p.timerOn || len(p.list) <= p.minSize { return } - p.timerIsActive = true + p.timerOn = true if p.timer == nil { - p.timer = time.AfterFunc(p.idleConnTTL, p.removeIdleConns) + p.timer = time.AfterFunc(p.cleanup, p.removeIdleConns) } else { - p.timer.Reset(p.idleConnTTL) + p.timer.Reset(p.cleanup) } } @@ -96,10 +97,6 @@ func (p *pool) removeIdleConns() { p.cond.L.Lock() defer p.cond.L.Unlock() - if p.down || len(p.list) <= p.minSize { - return - } - newLen := min(p.minSize, len(p.list)) for i, w := range p.list[newLen:] { w.Close() @@ -108,11 +105,11 @@ func (p *pool) removeIdleConns() { } p.list = p.list[:newLen] - p.timerIsActive = false + p.timerOn = false } func (p *pool) stopTimer() { - p.timerIsActive = false + p.timerOn = false if p.timer != nil { p.timer.Stop() } diff --git a/rueidis.go b/rueidis.go index 6d9591f8..1da3a178 100644 --- a/rueidis.go +++ b/rueidis.go @@ -133,13 +133,13 @@ type ClientOption struct { // WriteBufferEachConn is the size of the bufio.NewWriterSize for each connection, default to DefaultWriteBuffer (0.5 MiB). WriteBufferEachConn int - // IdleConnTTL is the duration for which a connection will be closed if it is idle. - // If IdleConnTTL is 0, then idle connections will not be closed. - IdleConnTTL time.Duration + // BlockingPoolCleanup is the duration for cleaning up idle connections. + // If BlockingPoolCleanup is 0, then idle connections will not be cleaned up. + BlockingPoolCleanup time.Duration // BlockingPoolMinSize is the minimum size of the connection pool // shared by blocking commands (ex BLPOP, XREAD with BLOCK). - // Only relevant if IdleConnTTL is not 0. This parameter limits - // the number of idle connections that can be removed by TTL. + // Only relevant if BlockingPoolCleanup is not 0. This parameter limits + // the number of idle connections that can be removed by BlockingPoolCleanup. BlockingPoolMinSize int // BlockingPoolSize is the size of the connection pool shared by blocking commands (ex BLPOP, XREAD with BLOCK).