Skip to content

Commit ad1e01c

Browse files
committed
feat: add dump function for current connections count
1 parent dc9c152 commit ad1e01c

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed

lib/Pool.js

+10
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,16 @@ Pool.prototype.escapeId = function escapeId(value) {
285285
return mysql.escapeId(value, false);
286286
};
287287

288+
Pool.prototype.dumpConnectionsCount = function () {
289+
return {
290+
allConnectionsCount : this._allConnections.length,
291+
freeConnectionsCount : this._freeConnections.length,
292+
connectionQueueCount : this._connectionQueue.length,
293+
acquiringConnectionsCount : this._acquiringConnections.length,
294+
queryingConnectionsCount : this._allConnections.length - this._freeConnections.length - this._acquiringConnections.length
295+
};
296+
};
297+
288298
function spliceConnection(array, connection) {
289299
var index;
290300
if ((index = array.indexOf(connection)) !== -1) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
var assert = require('assert');
2+
var common = require('../../common');
3+
4+
var server = common.createFakeServer();
5+
6+
server.listen(0, function (err) {
7+
assert.ifError(err);
8+
9+
var connectionLimit = 3;
10+
var pool = common.createPool({ port: server.port(), connectionLimit: connectionLimit } );
11+
assert.deepStrictEqual(connectionLimit, pool.config.connectionLimit);
12+
assert.deepStrictEqual(pool.dumpConnectionsCount(), {
13+
allConnectionsCount : 0,
14+
freeConnectionsCount : 0,
15+
connectionQueueCount : 0,
16+
acquiringConnectionsCount : 0,
17+
queryingConnectionsCount : 0
18+
});
19+
20+
var conn1, conn2, conn3, conn4, conn5;
21+
22+
pool.getConnection(function (err, connection) {
23+
assert.ifError(err);
24+
conn1 = connection;
25+
});
26+
27+
pool.getConnection(function (err, connection) {
28+
assert.ifError(err);
29+
conn2 = connection;
30+
});
31+
pool.getConnection(function (err, connection) {
32+
assert.ifError(err);
33+
conn3 = connection;
34+
});
35+
pool.getConnection(function (err, connection) {
36+
assert.ifError(err);
37+
conn4 = connection;
38+
});
39+
pool.getConnection(function (err, connection) {
40+
assert.ifError(err);
41+
conn5 = connection;
42+
});
43+
44+
setTimeout(function(){
45+
assert.ok(conn1);
46+
assert.ok(conn2);
47+
assert.ok(conn3);
48+
assert.ok(conn4 === undefined);
49+
assert.ok(conn5 === undefined);
50+
assert.deepStrictEqual(pool.dumpConnectionsCount(), {
51+
allConnectionsCount : 3,
52+
freeConnectionsCount : 0,
53+
connectionQueueCount : 2,
54+
acquiringConnectionsCount : 0,
55+
queryingConnectionsCount : 3
56+
});
57+
58+
pool.releaseConnection(conn1);
59+
assert.deepStrictEqual(pool.dumpConnectionsCount(), {
60+
allConnectionsCount : 3,
61+
freeConnectionsCount : 0,
62+
connectionQueueCount : 1,
63+
acquiringConnectionsCount : 1,
64+
queryingConnectionsCount : 2
65+
});
66+
67+
setTimeout(function() {
68+
assert.ok(conn4);
69+
assert.ok(conn5 === undefined);
70+
assert.deepStrictEqual(pool.dumpConnectionsCount(), {
71+
allConnectionsCount : 3,
72+
freeConnectionsCount : 0,
73+
connectionQueueCount : 1,
74+
acquiringConnectionsCount : 0,
75+
queryingConnectionsCount : 3
76+
});
77+
78+
pool.releaseConnection(conn2);
79+
pool.releaseConnection(conn3);
80+
pool.releaseConnection(conn4);
81+
assert.deepStrictEqual(pool.dumpConnectionsCount(), {
82+
allConnectionsCount : 3,
83+
freeConnectionsCount : 2,
84+
connectionQueueCount : 0,
85+
acquiringConnectionsCount : 1,
86+
queryingConnectionsCount : 0
87+
});
88+
89+
setTimeout(function() {
90+
assert.ok(conn5);
91+
pool.releaseConnection(conn5);
92+
assert.deepStrictEqual(pool.dumpConnectionsCount(), {
93+
allConnectionsCount : 3,
94+
freeConnectionsCount : 3,
95+
connectionQueueCount : 0,
96+
acquiringConnectionsCount : 0,
97+
queryingConnectionsCount : 0
98+
});
99+
pool.end(function(err){
100+
assert.ifError(err);
101+
assert.deepStrictEqual(pool.dumpConnectionsCount(), {
102+
allConnectionsCount : 0,
103+
freeConnectionsCount : 0,
104+
connectionQueueCount : 0,
105+
acquiringConnectionsCount : 0,
106+
queryingConnectionsCount : 0
107+
});
108+
server.destroy();
109+
});
110+
}, 100);
111+
}, 100);
112+
}, 100);
113+
114+
});

0 commit comments

Comments
 (0)