Skip to content
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

[WIP] Create function like TestOnBorrow that includes context #442

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion redis/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ type Pool struct {
// closed.
TestOnBorrow func(c Conn, t time.Time) error

// TestOnBorrowWithContext is the same as TestOnBorrow, but includes
// the context.
TestOnBorrowWithContext func(c Conn, t time.Time, ctx context.Context) error
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Context should always be the first parameter, I would also drop the With so its just TestOnBorrowContext as per Dial and DailContext


// Maximum number of idle connections in the pool.
MaxIdle int

Expand Down Expand Up @@ -354,7 +358,8 @@ func (p *Pool) get(ctx context.Context) (*poolConn, error) {
pc := p.idle.front
p.idle.popFront()
p.mu.Unlock()
if (p.TestOnBorrow == nil || p.TestOnBorrow(pc.c, pc.t) == nil) &&
if (p.TestOnBorrowWithContext == nil || p.TestOnBorrowWithContext(pc.c, pc.t, p.GetContext(ctx))) &&
(p.TestOnBorrow == nil || p.TestOnBorrow(pc.c, pc.t) == nil) &&
(p.MaxConnLifetime == 0 || nowFunc().Sub(pc.created) < p.MaxConnLifetime) {
return pc, nil
}
Expand Down
65 changes: 65 additions & 0 deletions redis/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,23 @@ func TestPoolBorrowCheck(t *testing.T) {
d.check("1", p, 10, 1, 0)
}

func TestPoolBorrowWithContextCheck(t *testing.T) {
d := poolDialer{t: t}
p := &redis.Pool{
MaxIdle: 2,
Dial: d.dial,
TestOnBorrowWithContext: func(redis.Conn, time.Time, context.Context) error { return redis.Error("BLAH") },
}
defer p.Close()

for i := 0; i < 10; i++ {
c := p.Get()
c.Do("PING")
c.Close()
}
d.check("1", p, 10, 1, 0)
}

func TestPoolMaxActive(t *testing.T) {
d := poolDialer{t: t}
p := &redis.Pool{
Expand Down Expand Up @@ -754,6 +771,54 @@ func TestLocking_TestOnBorrowFails_PoolDoesntCrash(t *testing.T) {
}
}

// Borrowing requires us to iterate over the idle connections, unlock the pool,
// and perform a blocking operation to check the connection still works. If
// TestOnBorrow fails, we must reacquire the lock and continue iteration. This
// test ensures that iteration will work correctly if multiple threads are
// iterating simultaneously.
func TestLocking_TestOnBorrowWithContextFails_PoolDoesntCrash(t *testing.T) {
const count = 100

// First we'll Create a pool where the pilfering of idle connections fails.
d := poolDialer{t: t}
p := &redis.Pool{
MaxIdle: count,
MaxActive: count,
Dial: d.dial,
TestOnBorrowWithContext: func(c redis.Conn, t time.Time, ctx context.Context) error {
return errors.New("No way back into the real world.")
},
}
defer p.Close()

// Fill the pool with idle connections.
conns := make([]redis.Conn, count)
for i := range conns {
conns[i] = p.Get()
}
for i := range conns {
conns[i].Close()
}

// Spawn a bunch of goroutines to thrash the pool.
var wg sync.WaitGroup
wg.Add(count)
for i := 0; i < count; i++ {
go func() {
c := p.Get()
if c.Err() != nil {
t.Errorf("pool get failed: %v", c.Err())
}
c.Close()
wg.Done()
}()
}
wg.Wait()
if d.dialed != count*2 {
t.Errorf("Expected %d dials, got %d", count*2, d.dialed)
}
}

func BenchmarkPoolGet(b *testing.B) {
b.StopTimer()
p := redis.Pool{Dial: func() (redis.Conn, error) { return redis.DialDefaultServer() }, MaxIdle: 2}
Expand Down