Immediately unref() maxLifetimeSeconds Timeout object to prevent blocking allowExitOnIdle (#2721)

This commit is contained in:
ChrisWritable 2022-05-12 17:05:02 -07:00 committed by GitHub
parent c7743646cd
commit 3ca56027d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 2 deletions

View File

@ -252,7 +252,7 @@ class Pool extends EventEmitter {
this.log('new client connected')
if (this.options.maxLifetimeSeconds !== 0) {
setTimeout(() => {
const maxLifetimeTimeout = setTimeout(() => {
this.log('ending client due to expired lifetime')
this._expired.add(client)
const idleIndex = this._idle.findIndex((idleItem) => idleItem.client === client)
@ -265,6 +265,9 @@ class Pool extends EventEmitter {
)
}
}, this.options.maxLifetimeSeconds * 1000)
maxLifetimeTimeout.unref()
client.once('end', () => clearTimeout(maxLifetimeTimeout))
}
return this._acquireClient(client, pendingItem, idleListener, true)

View File

@ -3,7 +3,7 @@ if (module === require.main) {
const allowExitOnIdle = process.env.ALLOW_EXIT_ON_IDLE === '1'
const Pool = require('../index')
const pool = new Pool({ idleTimeoutMillis: 200, ...(allowExitOnIdle ? { allowExitOnIdle: true } : {}) })
const pool = new Pool({ maxLifetimeSeconds: 2, idleTimeoutMillis: 200, ...(allowExitOnIdle ? { allowExitOnIdle: true } : {}) })
pool.query('SELECT NOW()', (err, res) => console.log('completed first'))
pool.on('remove', () => {
console.log('removed')