From e9f23666a788b4d2904d61baa01ad42489953678 Mon Sep 17 00:00:00 2001 From: robertpitt Date: Sat, 7 Sep 2024 14:39:20 +0100 Subject: [PATCH] Updated connection cleanup process to ensure we cleanup connections that have expired as well as the connections that exceed the config.maxIdle setting --- lib/pool.js | 7 ++++--- .../integration/test-pool-release-idle-connection.test.cjs | 4 +++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/pool.js b/lib/pool.js index 1b4993ff1a..dc638d743e 100644 --- a/lib/pool.js +++ b/lib/pool.js @@ -200,9 +200,10 @@ class Pool extends EventEmitter { this._removeIdleTimeoutConnectionsTimer = setTimeout(() => { try { while ( - this._freeConnections.length > this.config.maxIdle && - Date.now() - this._freeConnections.get(0).lastActiveTime > - this.config.idleTimeout + this._freeConnections.length > this.config.maxIdle || + (this._freeConnections.length > 0 && + Date.now() - this._freeConnections.get(0).lastActiveTime > + this.config.idleTimeout) ) { this._freeConnections.get(0).destroy(); } diff --git a/test/integration/test-pool-release-idle-connection.test.cjs b/test/integration/test-pool-release-idle-connection.test.cjs index 7155c28f1a..48c9f1dbdb 100644 --- a/test/integration/test-pool-release-idle-connection.test.cjs +++ b/test/integration/test-pool-release-idle-connection.test.cjs @@ -39,7 +39,9 @@ pool.getConnection((err1, connection1) => { connection4.destroy(); pool.end(); }); - }, 7000); + // Setting the time to a lower value than idleTimeout will ensure that the connection is not considered idle + // during our assertions + }, 4000); }); }); });