Skip to content
This repository has been archived by the owner on Dec 30, 2019. It is now read-only.

Prevent requeuing a broken client #119

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
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function throwOnRelease () {

function release (client, err) {
client.release = throwOnRelease
if (err || this.ending) {
if (err || this.ending || !client._queryable || client._ending) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It would be nice if the queryable state of a client is retrievable via a public method.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alternatively re-adding a not queryable client could throw instead of silently re-adding.

this._remove(client)
this._pulseQueue()
return
Expand Down
34 changes: 34 additions & 0 deletions test/error-handling.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,40 @@ describe('pool error handling', function () {
})
})

describe('releasing a not queryable client', () => {
it('removes the client from the pool', (done) => {
const pool = new Pool({ max: 1 })
const connectionError = new Error('connection failed')

pool.once('error', () => {
// Ignore error on pool
})

pool.connect((err, client) => {
expect(err).to.be(undefined)

client.once('error', (err) => {
expect(err).to.eql(connectionError)

// Releasing the client should remove it from the pool,
// whether called with an error or not
client.release()

// Verify that the pool is still usuable and new client has been
// created
pool.query('SELECT $1::text as name', ['brianc'], (err, res) => {
expect(err).to.be(undefined)
expect(res.rows).to.eql([{ name: 'brianc' }])

pool.end(done)
})
})

client.emit('error', connectionError)
})
})
})

describe('pool with lots of errors', () => {
it('continues to work and provide new clients', co.wrap(function * () {
const pool = new Pool({ max: 1 })
Expand Down