1
0
mirror of https://github.com/redis/go-redis.git synced 2025-08-08 23:42:06 +03:00

feat: recover addIdleConn may occur panic (#2445)

* feat: recover addIdleConn may occur panic

Signed-off-by: monkey92t <golang@88.com>

* fix test race

Signed-off-by: monkey92t <golang@88.com>

---------

Signed-off-by: monkey92t <golang@88.com>
Co-authored-by: Nedyalko Dyakov <1547186+ndyakov@users.noreply.github.com>
This commit is contained in:
Monkey
2025-08-05 20:31:58 +08:00
committed by GitHub
parent 4767d9dfaf
commit 6a48d3fec1
3 changed files with 40 additions and 0 deletions

View File

@@ -12,3 +12,13 @@ func (cn *Conn) SetCreatedAt(tm time.Time) {
func (cn *Conn) NetConn() net.Conn {
return cn.netConn
}
func (p *ConnPool) CheckMinIdleConns() {
p.connsMu.Lock()
p.checkMinIdleConns()
p.connsMu.Unlock()
}
func (p *ConnPool) QueueLen() int {
return len(p.queue)
}

View File

@@ -130,6 +130,18 @@ func (p *ConnPool) checkMinIdleConns() {
p.idleConnsLen++
go func() {
defer func() {
if err := recover(); err != nil {
p.connsMu.Lock()
p.poolSize--
p.idleConnsLen--
p.connsMu.Unlock()
p.freeTurn()
internal.Logger.Printf(context.Background(), "addIdleConn panic: %+v", err)
}
}()
err := p.addIdleConn()
if err != nil && err != ErrClosed {
p.connsMu.Lock()

View File

@@ -361,6 +361,24 @@ var _ = Describe("race", func() {
Expect(stats.TotalConns).To(Equal(uint32(opt.PoolSize)))
})
It("recover addIdleConn panic", func() {
opt := &pool.Options{
Dialer: func(ctx context.Context) (net.Conn, error) {
panic("test panic")
},
PoolSize: 100,
MinIdleConns: 30,
}
p := pool.NewConnPool(opt)
p.CheckMinIdleConns()
Eventually(func() bool {
state := p.Stats()
return state.TotalConns == 0 && state.IdleConns == 0 && p.QueueLen() == 0
}, "3s", "50ms").Should(BeTrue())
})
It("wait", func() {
opt := &pool.Options{
Dialer: func(ctx context.Context) (net.Conn, error) {