Skip to content

Commit

Permalink
feat: add dump function for current connections count
Browse files Browse the repository at this point in the history
  • Loading branch information
qile222 committed Jul 27, 2023
1 parent dc9c152 commit a67f0a8
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/Pool.js
Expand Up @@ -285,6 +285,16 @@ Pool.prototype.escapeId = function escapeId(value) {
return mysql.escapeId(value, false);
};

Pool.prototype.getConnectionCounts = function () {
return {
allConnectionsCount : this._allConnections.length,
idleConnectionsCount : this._freeConnections.length,
connectionQueueCount : this._connectionQueue.length,
acquiringConnectionsCount : this._acquiringConnections.length,
busyConnectionsCount : this._allConnections.length - this._freeConnections.length - this._acquiringConnections.length
};
};

function spliceConnection(array, connection) {
var index;
if ((index = array.indexOf(connection)) !== -1) {
Expand Down
100 changes: 100 additions & 0 deletions test/unit/pool/test-get-connection-counts.js
@@ -0,0 +1,100 @@
var assert = require('assert');
var common = require('../../common');

var server = common.createFakeServer();

server.listen(0, function (err) {
assert.ifError(err);

var connectionLimit = 3;
var pool = common.createPool({ port: server.port(), connectionLimit: connectionLimit } );
assert.equal(connectionLimit, pool.config.connectionLimit);
assert.equal(pool.getConnectionCounts().allConnectionsCount, 0);
assert.equal(pool.getConnectionCounts().idleConnectionsCount, 0);
assert.equal(pool.getConnectionCounts().connectionQueueCount, 0);
assert.equal(pool.getConnectionCounts().acquiringConnectionsCount, 0);
assert.equal(pool.getConnectionCounts().busyConnectionsCount, 0);

var conn1, conn2, conn3, conn4, conn5;

pool.getConnection(function (err, connection) {
assert.ifError(err);
conn1 = connection;
});

pool.getConnection(function (err, connection) {
assert.ifError(err);
conn2 = connection;
});
pool.getConnection(function (err, connection) {
assert.ifError(err);
conn3 = connection;
});
pool.getConnection(function (err, connection) {
assert.ifError(err);
conn4 = connection;
});
pool.getConnection(function (err, connection) {
assert.ifError(err);
conn5 = connection;
});

setTimeout(function(){
assert.ok(conn1);
assert.ok(conn2);
assert.ok(conn3);
assert.ok(conn4 === undefined);
assert.ok(conn5 === undefined);
assert.equal(pool.getConnectionCounts().allConnectionsCount, 3);
assert.equal(pool.getConnectionCounts().idleConnectionsCount, 0);
assert.equal(pool.getConnectionCounts().connectionQueueCount, 2);
assert.equal(pool.getConnectionCounts().acquiringConnectionsCount, 0);
assert.equal(pool.getConnectionCounts().busyConnectionsCount, 3);

pool.releaseConnection(conn1);
assert.equal(pool.getConnectionCounts().allConnectionsCount, 3);
assert.equal(pool.getConnectionCounts().idleConnectionsCount, 0);
assert.equal(pool.getConnectionCounts().connectionQueueCount, 1);
assert.equal(pool.getConnectionCounts().acquiringConnectionsCount, 1);
assert.equal(pool.getConnectionCounts().busyConnectionsCount, 2);

setTimeout(function() {
assert.ok(conn4);
assert.ok(conn5 === undefined);
assert.equal(pool.getConnectionCounts().allConnectionsCount, 3);
assert.equal(pool.getConnectionCounts().idleConnectionsCount, 0);
assert.equal(pool.getConnectionCounts().connectionQueueCount, 1);
assert.equal(pool.getConnectionCounts().acquiringConnectionsCount, 0);
assert.equal(pool.getConnectionCounts().busyConnectionsCount, 3);

pool.releaseConnection(conn2);
pool.releaseConnection(conn3);
pool.releaseConnection(conn4);
assert.equal(pool.getConnectionCounts().allConnectionsCount, 3);
assert.equal(pool.getConnectionCounts().idleConnectionsCount, 2);
assert.equal(pool.getConnectionCounts().connectionQueueCount, 0);
assert.equal(pool.getConnectionCounts().acquiringConnectionsCount, 1);
assert.equal(pool.getConnectionCounts().busyConnectionsCount, 0);

setTimeout(function() {
assert.ok(conn5);
pool.releaseConnection(conn5);
assert.equal(pool.getConnectionCounts().allConnectionsCount, 3);
assert.equal(pool.getConnectionCounts().idleConnectionsCount, 3);
assert.equal(pool.getConnectionCounts().connectionQueueCount, 0);
assert.equal(pool.getConnectionCounts().acquiringConnectionsCount, 0);
assert.equal(pool.getConnectionCounts().busyConnectionsCount, 0);
pool.end(function(err){
assert.ifError(err);
assert.equal(pool.getConnectionCounts().allConnectionsCount, 0);
assert.equal(pool.getConnectionCounts().idleConnectionsCount, 0);
assert.equal(pool.getConnectionCounts().connectionQueueCount, 0);
assert.equal(pool.getConnectionCounts().acquiringConnectionsCount, 0);
assert.equal(pool.getConnectionCounts().busyConnectionsCount, 0);
server.destroy();
});
}, 100);
}, 100);
}, 100);

});

0 comments on commit a67f0a8

Please sign in to comment.