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

Emit a 'release' event when a connection is released back to the pool #2845

Merged
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
2 changes: 2 additions & 0 deletions packages/pg-pool/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,8 @@ class Pool extends EventEmitter {

client._poolUseCount = (client._poolUseCount || 0) + 1

this.emit('release', err, client)

// TODO(bmc): expose a proper, public interface _queryable and _ending
if (err || this.ending || !client._queryable || client._ending || client._poolUseCount >= this.options.maxUses) {
if (client._poolUseCount >= this.options.maxUses) {
Expand Down
36 changes: 36 additions & 0 deletions packages/pg-pool/test/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,42 @@ describe('events', function () {
}, 100)
})

it('emits release every time a client is released', function (done) {
const pool = new Pool()
let releaseCount = 0
pool.on('release', function (err, client) {
expect(err instanceof Error).not.to.be(true)
expect(client).to.be.ok()
releaseCount++
})
for (let i = 0; i < 10; i++) {
pool.connect(function (err, client, release) {
if (err) return done(err)
release()
})
pool.query('SELECT now()')
}
setTimeout(function () {
expect(releaseCount).to.be(20)
pool.end(done)
}, 100)
})

it('emits release with an error if client is released due to an error', function (done) {
const pool = new Pool()
pool.connect(function (err, client, release) {
expect(err).to.equal(undefined)
const releaseError = new Error('problem')
pool.once('release', function (err, errClient) {
console.log(err, errClient)
expect(err).to.equal(releaseError)
expect(errClient).to.equal(client)
pool.end(done)
})
release(releaseError)
})
})

it('emits error and client if an idle client in the pool hits an error', function (done) {
const pool = new Pool()
pool.connect(function (err, client) {
Expand Down