Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: gracefully close pool connections #3180

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions lib/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class Pool extends EventEmitter {
}
for (let i = 0; i < this._allConnections.length; i++) {
connection = this._allConnections.get(i);
connection._realEnd(endCB);
connection.end(endCB);
}
}

Expand Down Expand Up @@ -205,7 +205,7 @@ class Pool extends EventEmitter {
Date.now() - this._freeConnections.get(0).lastActiveTime >
this.config.idleTimeout)
) {
this._freeConnections.get(0).destroy();
this._freeConnections.get(0).end();
}
} finally {
this._removeIdleTimeoutConnections();
Expand Down
17 changes: 3 additions & 14 deletions lib/pool_connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,9 @@ class PoolConnection extends Connection {
return new PromisePoolConnection(this, promiseImpl);
}

end() {
const err = new Error(
'Calling conn.end() to release a pooled connection is ' +
'deprecated. In next version calling conn.end() will be ' +
'restored to default conn.end() behavior. Use ' +
'conn.release() instead.'
);
this.emit('warn', err);
// eslint-disable-next-line no-console
console.warn(err.message);
this.release();
end(callback) {
this._removeFromPool();
super.end(callback);
}

destroy() {
Expand All @@ -64,6 +56,3 @@ class PoolConnection extends Connection {

PoolConnection.statementKey = Connection.statementKey;
module.exports = PoolConnection;

// TODO: Remove this when we are removing PoolConnection#end
PoolConnection.prototype._realEnd = Connection.prototype.end;
41 changes: 41 additions & 0 deletions test/test-pool-releases-connections-gracefully.test.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';
const createPool = require('../common.test.cjs').createPool;
const { assert } = require('poku');

/**
* This test case tests that the pool releases connections gracefully after the idle timeout has passed.
*
* @see https://github.com/sidorares/node-mysql2/issues/3148
*/

const pool = new createPool({
connectionLimit: 3,
maxIdle: 2,
idleTimeout: 1000,
});

let connection1Ended = false;
let connection2Ended = false;
let connection3Ended = false;

pool.getConnection((_err1, connection1) => {
pool.getConnection((_err2, connection2) => {
pool.getConnection((_err3, connection3) => {
connection1.stream.on('end', () => (connection1Ended = true));
connection2.stream.on('end', () => (connection2Ended = true));
connection3.stream.on('end', () => (connection3Ended = true));

connection1.release();
connection2.release();
connection3.release();

setTimeout(() => {
assert(connection1Ended, 'connection1 should have ended');
assert(connection2Ended, 'connection2 should have ended');
assert(connection3Ended, 'connection3 should have ended');

pool.end();
}, 2000);
});
});
});
Loading