Merge branch 'master' into add-name

This commit is contained in:
Manni Wood 2016-10-06 09:07:42 -04:00
commit 9041885e3f
3 changed files with 8 additions and 4 deletions

View File

@ -12,6 +12,7 @@
* Add "char" type support (Manni Wood) * Add "char" type support (Manni Wood)
* Add NullOid type (Manni Wood) * Add NullOid type (Manni Wood)
* Add json/jsonb binary support to allow use with CopyTo * Add json/jsonb binary support to allow use with CopyTo
* Add named error ErrAcquireTimeout (Alexander Staubo)
# 2.9.0 (August 26, 2016) # 2.9.0 (August 26, 2016)

View File

@ -40,6 +40,9 @@ type ConnPoolStat struct {
AvailableConnections int // unused live connections AvailableConnections int // unused live connections
} }
// ErrAcquireTimeout occurs when an attempt to acquire a connection times out.
var ErrAcquireTimeout = errors.New("timeout acquiring connection from pool")
// NewConnPool creates a new ConnPool. config.ConnConfig is passed through to // NewConnPool creates a new ConnPool. config.ConnConfig is passed through to
// Connect directly. // Connect directly.
func NewConnPool(config ConnPoolConfig) (p *ConnPool, err error) { func NewConnPool(config ConnPoolConfig) (p *ConnPool, err error) {
@ -131,7 +134,7 @@ func (p *ConnPool) acquire(deadline *time.Time) (*Conn, error) {
// Make sure the deadline (if it is) has not passed yet // Make sure the deadline (if it is) has not passed yet
if p.deadlinePassed(deadline) { if p.deadlinePassed(deadline) {
return nil, errors.New("Timeout: Acquire connection timeout") return nil, ErrAcquireTimeout
} }
// If there is a deadline then start a timeout timer // If there is a deadline then start a timeout timer
@ -164,7 +167,7 @@ func (p *ConnPool) acquire(deadline *time.Time) (*Conn, error) {
// Wait until there is an available connection OR room to create a new connection // Wait until there is an available connection OR room to create a new connection
for len(p.availableConnections) == 0 && len(p.allConnections)+p.inProgressConnects == p.maxConnections { for len(p.availableConnections) == 0 && len(p.allConnections)+p.inProgressConnects == p.maxConnections {
if p.deadlinePassed(deadline) { if p.deadlinePassed(deadline) {
return nil, errors.New("Timeout: All connections in pool are busy") return nil, ErrAcquireTimeout
} }
p.cond.Wait() p.cond.Wait()
} }

View File

@ -276,8 +276,8 @@ func TestPoolWithAcquireTimeoutSet(t *testing.T) {
// ... then try to consume 1 more. It should fail after a short timeout. // ... then try to consume 1 more. It should fail after a short timeout.
_, timeTaken, err := acquireWithTimeTaken(pool) _, timeTaken, err := acquireWithTimeTaken(pool)
if err == nil || err.Error() != "Timeout: All connections in pool are busy" { if err == nil || err != pgx.ErrAcquireTimeout {
t.Fatalf("Expected error to be 'Timeout: All connections in pool are busy', instead it was '%v'", err) t.Fatalf("Expected error to be pgx.ErrAcquireTimeout, instead it was '%v'", err)
} }
if timeTaken < connAllocTimeout { if timeTaken < connAllocTimeout {
t.Fatalf("Expected connection allocation time to be at least %v, instead it was '%v'", connAllocTimeout, timeTaken) t.Fatalf("Expected connection allocation time to be at least %v, instead it was '%v'", connAllocTimeout, timeTaken)