Skip to content

after the connection pool is closed, no new connections should be added #1863

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 19, 2021
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
4 changes: 3 additions & 1 deletion internal/pool/export_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package pool

import "time"
import (
"time"
)

func (cn *Conn) SetCreatedAt(tm time.Time) {
cn.createdAt = tm
Expand Down
36 changes: 29 additions & 7 deletions internal/pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,10 @@ func (p *ConnPool) checkMinIdleConns() {
for p.poolSize < p.opt.PoolSize && p.idleConnsLen < p.opt.MinIdleConns {
p.poolSize++
p.idleConnsLen++

go func() {
err := p.addIdleConn()
if err != nil {
if err != nil && err != ErrClosed {
p.connsMu.Lock()
p.poolSize--
p.idleConnsLen--
Expand All @@ -140,9 +141,16 @@ func (p *ConnPool) addIdleConn() error {
}

p.connsMu.Lock()
defer p.connsMu.Unlock()

// It is not allowed to add new connections to the closed connection pool.
if p.closed() {
_ = cn.Close()
return ErrClosed
}

p.conns = append(p.conns, cn)
p.idleConns = append(p.idleConns, cn)
p.connsMu.Unlock()
return nil
}

Expand All @@ -157,6 +165,14 @@ func (p *ConnPool) newConn(ctx context.Context, pooled bool) (*Conn, error) {
}

p.connsMu.Lock()
defer p.connsMu.Unlock()

// It is not allowed to add new connections to the closed connection pool.
if p.closed() {
_ = cn.Close()
return nil, ErrClosed
}

p.conns = append(p.conns, cn)
if pooled {
// If pool is full remove the cn on next Put.
Expand All @@ -166,7 +182,6 @@ func (p *ConnPool) newConn(ctx context.Context, pooled bool) (*Conn, error) {
p.poolSize++
}
}
p.connsMu.Unlock()

return cn, nil
}
Expand Down Expand Up @@ -237,9 +252,13 @@ func (p *ConnPool) Get(ctx context.Context) (*Conn, error) {

for {
p.connsMu.Lock()
cn := p.popIdle()
cn, err := p.popIdle()
p.connsMu.Unlock()

if err != nil {
return nil, err
}

if cn == nil {
break
}
Expand Down Expand Up @@ -308,10 +327,13 @@ func (p *ConnPool) freeTurn() {
<-p.queue
}

func (p *ConnPool) popIdle() *Conn {
func (p *ConnPool) popIdle() (*Conn, error) {
if p.closed() {
return nil, ErrClosed
}
n := len(p.idleConns)
if n == 0 {
return nil
return nil, nil
}

var cn *Conn
Expand All @@ -326,7 +348,7 @@ func (p *ConnPool) popIdle() *Conn {
}
p.idleConnsLen--
p.checkMinIdleConns()
return cn
return cn, nil
}

func (p *ConnPool) Put(ctx context.Context, cn *Conn) {
Expand Down
38 changes: 38 additions & 0 deletions internal/pool/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pool_test

import (
"context"
"net"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -30,6 +31,43 @@ var _ = Describe("ConnPool", func() {
connPool.Close()
})

It("should safe close", func() {
const minIdleConns = 10

var (
wg sync.WaitGroup
closedChan = make(chan struct{})
)
wg.Add(minIdleConns)
connPool = pool.NewConnPool(&pool.Options{
Dialer: func(ctx context.Context) (net.Conn, error) {
wg.Done()
<-closedChan
return &net.TCPConn{}, nil
},
PoolSize: 10,
PoolTimeout: time.Hour,
IdleTimeout: time.Millisecond,
IdleCheckFrequency: time.Millisecond,
MinIdleConns: minIdleConns,
})
wg.Wait()
Expect(connPool.Close()).NotTo(HaveOccurred())
close(closedChan)

// We wait for 1 second and believe that checkMinIdleConns has been executed.
time.Sleep(time.Second)

Expect(connPool.Stats()).To(Equal(&pool.Stats{
Hits: 0,
Misses: 0,
Timeouts: 0,
TotalConns: 0,
IdleConns: 0,
StaleConns: 0,
}))
})

It("should unblock client when conn is removed", func() {
// Reserve one connection.
cn, err := connPool.Get(ctx)
Expand Down
2 changes: 1 addition & 1 deletion internal/proto/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const (

//------------------------------------------------------------------------------

const Nil = RedisError("redis: nil")
const Nil = RedisError("redis: nil") // nolint:errname

type RedisError string

Expand Down