mirror of https://github.com/go-redis/redis.git
protect pollSize and idleConnsLen with mutex (#1105)
Fix poolSize and idleConnsLen when conn can't be created
This commit is contained in:
parent
d190c8191e
commit
6cd6cd828f
internal/pool
|
@ -92,9 +92,7 @@ func NewConnPool(opt *Options) *ConnPool {
|
|||
idleConns: make([]*Conn, 0, opt.PoolSize),
|
||||
}
|
||||
|
||||
for i := 0; i < opt.MinIdleConns; i++ {
|
||||
p.checkMinIdleConns()
|
||||
}
|
||||
p.checkMinIdleConns()
|
||||
|
||||
if opt.IdleTimeout > 0 && opt.IdleCheckFrequency > 0 {
|
||||
go p.reaper(opt.IdleCheckFrequency)
|
||||
|
@ -107,7 +105,7 @@ func (p *ConnPool) checkMinIdleConns() {
|
|||
if p.opt.MinIdleConns == 0 {
|
||||
return
|
||||
}
|
||||
if p.poolSize < p.opt.PoolSize && p.idleConnsLen < p.opt.MinIdleConns {
|
||||
for p.poolSize < p.opt.PoolSize && p.idleConnsLen < p.opt.MinIdleConns {
|
||||
p.poolSize++
|
||||
p.idleConnsLen++
|
||||
go p.addIdleConn()
|
||||
|
@ -117,6 +115,10 @@ func (p *ConnPool) checkMinIdleConns() {
|
|||
func (p *ConnPool) addIdleConn() {
|
||||
cn, err := p.newConn(context.TODO(), true)
|
||||
if err != nil {
|
||||
p.connsMu.Lock()
|
||||
p.poolSize--
|
||||
p.idleConnsLen--
|
||||
p.connsMu.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue