Skip to content
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
5 changes: 4 additions & 1 deletion packages/pg-pool/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion packages/pg-pool/test/idle-timeout-exit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down