Add Pool.AcquireAllIdle()

pull/483/head
Jack Christensen 2019-04-27 08:02:52 -05:00
parent 46a92b5cd4
commit 27b8876ea3
2 changed files with 47 additions and 0 deletions

View File

@ -88,6 +88,17 @@ func (p *Pool) Acquire(ctx context.Context) (*Conn, error) {
return &Conn{res: res}, nil
}
// AcquireAllIdle atomically acquires all currently idle connections. Its intended use is for health check and
// keep-alive functionality. It does not update pool statistics.
func (p *Pool) AcquireAllIdle() []*Conn {
resources := p.p.AcquireAllIdle()
conns := make([]*Conn, len(resources))
for i := range conns {
conns[i] = &Conn{res: resources[i]}
}
return conns
}
func (p *Pool) Stat() *Stat {
return &Stat{s: p.p.Stat()}
}

View File

@ -51,6 +51,42 @@ func TestPoolAcquireAndConnRelease(t *testing.T) {
c.Release()
}
func TestPoolAcquireAllIdle(t *testing.T) {
t.Parallel()
db, err := pool.Connect(context.Background(), os.Getenv("PGX_TEST_DATABASE"))
require.NoError(t, err)
defer db.Close()
conns := db.AcquireAllIdle()
assert.Len(t, conns, 1)
for _, c := range conns {
c.Release()
}
waitForReleaseToComplete()
conns = make([]*pool.Conn, 3)
for i := range conns {
conns[i], err = db.Acquire(context.Background())
assert.NoError(t, err)
}
for _, c := range conns {
if c != nil {
c.Release()
}
}
waitForReleaseToComplete()
conns = db.AcquireAllIdle()
assert.Len(t, conns, 3)
for _, c := range conns {
c.Release()
}
}
func TestPoolExec(t *testing.T) {
t.Parallel()