mirror of
https://github.com/brianc/node-postgres.git
synced 2026-01-18 15:55:05 +00:00
Emit a 'release' event when a connection is released back to the pool (#2845)
This commit is contained in:
parent
8804e5caaf
commit
810b125581
@ -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) {
|
||||
|
||||
@ -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) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user