mirror of https://github.com/go-redis/redis.git
fix: limit the number of connections created (#2441)
* fix: limit the number of connections created Signed-off-by: monkey92t <golang@88.com>
This commit is contained in:
parent
0d306237c7
commit
3532f2a414
|
@ -112,6 +112,8 @@ func (p *ConnPool) checkMinIdleConns() {
|
|||
return
|
||||
}
|
||||
for p.poolSize < p.cfg.PoolSize && p.idleConnsLen < p.cfg.MinIdleConns {
|
||||
select {
|
||||
case p.queue <- struct{}{}:
|
||||
p.poolSize++
|
||||
p.idleConnsLen++
|
||||
|
||||
|
@ -123,7 +125,12 @@ func (p *ConnPool) checkMinIdleConns() {
|
|||
p.idleConnsLen--
|
||||
p.connsMu.Unlock()
|
||||
}
|
||||
|
||||
p.freeTurn()
|
||||
}()
|
||||
default:
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -401,6 +408,7 @@ func (p *ConnPool) removeConn(cn *Conn) {
|
|||
break
|
||||
}
|
||||
}
|
||||
atomic.AddUint32(&p.stats.StaleConns, 1)
|
||||
}
|
||||
|
||||
func (p *ConnPool) closeConn(cn *Conn) error {
|
||||
|
|
|
@ -327,4 +327,30 @@ var _ = Describe("race", func() {
|
|||
}
|
||||
})
|
||||
})
|
||||
|
||||
It("limit the number of connections", func() {
|
||||
opt := &pool.Options{
|
||||
Dialer: func(ctx context.Context) (net.Conn, error) {
|
||||
return &net.TCPConn{}, nil
|
||||
},
|
||||
PoolSize: 1000,
|
||||
MinIdleConns: 50,
|
||||
PoolTimeout: 3 * time.Second,
|
||||
}
|
||||
p := pool.NewConnPool(opt)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
for i := 0; i < opt.PoolSize; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
_, _ = p.Get(ctx)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
stats := p.Stats()
|
||||
Expect(stats.IdleConns).To(Equal(uint32(0)))
|
||||
Expect(stats.TotalConns).To(Equal(uint32(opt.PoolSize)))
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue