Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions internal/pool/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
12 changes: 12 additions & 0 deletions internal/pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
18 changes: 18 additions & 0 deletions internal/pool/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading