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

add Warning Full Connection Pool (event) #802

Closed
wants to merge 3 commits into from
Closed
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
12 changes: 11 additions & 1 deletion lib/Pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var Connection = require('./Connection');
var EventEmitter = require('events').EventEmitter;
var Util = require('util');
var PoolConnection = require('./PoolConnection');
var WarningFullConnectionPoolEvent = require('./WarningFullConnectionPoolEvent');

module.exports = Pool;

Expand Down Expand Up @@ -30,14 +31,19 @@ Pool.prototype.getConnection = function (cb) {

if (this._freeConnections.length > 0) {
connection = this._freeConnections.shift();

if(this.config.warningFullConnectionPool) {
connection._stack = new Error().stack;
}
return process.nextTick(function(){
cb(null, connection);
});
}

if (this.config.connectionLimit === 0 || this._allConnections.length < this.config.connectionLimit) {
connection = new PoolConnection(this, { config: this.config.connectionConfig });
if(this.config.warningFullConnectionPool) {
connection._stack = new Error().stack;
}

this._allConnections.push(connection);

Expand All @@ -64,6 +70,10 @@ Pool.prototype.getConnection = function (cb) {
return cb(new Error('Queue limit reached.'));
}

if(this.config.warningFullConnectionPool) {
this.emit('warningFullConnectionPool', new WarningFullConnectionPoolEvent(this));
}

if (cb && process.domain)
cb = process.domain.bind(cb);
this._connectionQueue.push(cb);
Expand Down
3 changes: 3 additions & 0 deletions lib/PoolConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ function PoolConfig(options) {
this.queueLimit = (options.queueLimit === undefined)
? 0
: Number(options.queueLimit);
this.warningFullConnectionPool = (options.warningFullConnectionPool === undefined)
? false
: Boolean(options.warningFullConnectionPool);
}
9 changes: 9 additions & 0 deletions lib/PoolConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,12 @@ PoolConnection.prototype._removeFromPool = function(connection) {

pool._removeConnection(this);
};

PoolConnection.prototype.query = function(sql, values, cb) {
if(this._pool.config.warningFullConnectionPool) {
this._lastQuery = sql;
this._lastQueryStack = new Error().stack;
}
return PoolConnection.super_.prototype.query.apply(this, [sql, values, cb]);
};

42 changes: 42 additions & 0 deletions lib/WarningFullConnectionPoolEvent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
module.exports = WarningFullConnectionPoolEvent;

function WarningFullConnectionPoolEvent(pool) {
this._pool = pool;
this._data = [];

for(var i=0; i<pool._allConnections.length; i++) {
this._data.push(new ConnectionData(pool._allConnections[i]));
}
}

WarningFullConnectionPoolEvent.prototype.getData = function () {
return this._data;
}

function ConnectionData(conn) {
this._conn = conn;
this._stack = conn._stack;
this._lastQuery = conn._lastQuery;
this._lastQueryStack = conn._lastQueryStack;
}

ConnectionData.prototype.getStack = function () {
return this._stack;
}

ConnectionData.prototype.getLastQuery = function () {
return this._lastQuery;
}

ConnectionData.prototype.getLastQueryStack = function () {
return this._lastQueryStack;
}

ConnectionData.prototype.toString = function () {
var str = "";
str += "Full Mysql Connection Pool - lastConnectionData\n";
str += "stack : " + this.getStack() + "\n";
str += "query : " + this.getLastQuery() + "\n";
str += "query stack : " + this.getLastQueryStack() + "\n";
return str;
}