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

This commit is contained in:
Ryan B. Harvey 2023-03-06 12:32:13 -06:00 committed by GitHub
parent 8804e5caaf
commit 810b125581
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 0 deletions

View File

@ -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) {

View File

@ -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) {