Skip to content

Commit 3ca5602

Browse files
Immediately unref() maxLifetimeSeconds Timeout object to prevent blocking allowExitOnIdle (#2721)
1 parent c774364 commit 3ca5602

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

packages/pg-pool/index.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ class Pool extends EventEmitter {
252252
this.log('new client connected')
253253

254254
if (this.options.maxLifetimeSeconds !== 0) {
255-
setTimeout(() => {
255+
const maxLifetimeTimeout = setTimeout(() => {
256256
this.log('ending client due to expired lifetime')
257257
this._expired.add(client)
258258
const idleIndex = this._idle.findIndex((idleItem) => idleItem.client === client)
@@ -265,6 +265,9 @@ class Pool extends EventEmitter {
265265
)
266266
}
267267
}, this.options.maxLifetimeSeconds * 1000)
268+
269+
maxLifetimeTimeout.unref()
270+
client.once('end', () => clearTimeout(maxLifetimeTimeout))
268271
}
269272

270273
return this._acquireClient(client, pendingItem, idleListener, true)

packages/pg-pool/test/idle-timeout-exit.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ if (module === require.main) {
33
const allowExitOnIdle = process.env.ALLOW_EXIT_ON_IDLE === '1'
44
const Pool = require('../index')
55

6-
const pool = new Pool({ idleTimeoutMillis: 200, ...(allowExitOnIdle ? { allowExitOnIdle: true } : {}) })
6+
const pool = new Pool({ maxLifetimeSeconds: 2, idleTimeoutMillis: 200, ...(allowExitOnIdle ? { allowExitOnIdle: true } : {}) })
77
pool.query('SELECT NOW()', (err, res) => console.log('completed first'))
88
pool.on('remove', () => {
99
console.log('removed')

0 commit comments

Comments
 (0)