Skip to content

Fix issue #473 (Make Pool emit a "connection" event) #474

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

Closed
wants to merge 4 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
11 changes: 10 additions & 1 deletion lib/Pool.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@

var Mysql = require('../');
var Connection = require('./Connection');
var EventEmitter = require('events').EventEmitter;
var Util = require('util');

module.exports = Pool;
Util.inherits(Pool, EventEmitter);
function Pool(options) {
EventEmitter.call(this);
this.config = options.config;
this.config.connectionConfig.pool = this;

Expand Down Expand Up @@ -34,11 +38,16 @@ Pool.prototype.getConnection = function(cb) {
else if (err) {
cb(err);
} else {
self.emit('connection', null, connection);
cb(null, connection);
}
});
} else if (this.config.waitForConnections) {
this._connectionQueue.push(cb);
if (this.config.queueLimit && this._connectionQueue.length >= this.config.queueLimit) {
cb(new Error('Queue limit reached.'))
} else {
this._connectionQueue.push(cb);
}
} else {
cb(new Error('No connections available.'));
}
Expand Down
3 changes: 3 additions & 0 deletions lib/PoolConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ function PoolConfig(options) {
this.connectionLimit = (options.connectionLimit === undefined)
? 10
: Number(options.connectionLimit);
this.queueLimit = (options.queueLimit === undefined)
? 0
: Number(options.queueLimit);
}
4 changes: 3 additions & 1 deletion test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ common.createConnection = function(config) {

common.createPool = function(config) {
config = mergeTestConfig(config);
config.createConnection = common.createConnection;
if (!config.createConnection) {
config.createConnection = common.createConnection;
}
return Mysql.createPool(config);
};

Expand Down
15 changes: 15 additions & 0 deletions test/integration/pool/test-create-connection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var common = require('../../common');
var assert = require('assert');
var Connection = require(common.lib + '/Connection');
var pool = common.createPool({
createConnection: function() {
var connection = common.createConnection()
connection.query('SET SESSION sql_mode="STRICT_ALL_TABLES"')
return connection
}
});

pool.getConnection(function(err, connection) {
if (err) throw err;
pool.end()
});
28 changes: 28 additions & 0 deletions test/integration/pool/test-queue-limit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var common = require('../../common');
var assert = require('assert');
var pool = common.createPool({
connectionLimit : 1,
queueLimit : 1,
waitForConnections : true
});

// First connection we get right away
pool.getConnection(function(err, connection) {
connection.end()
})

// Second connection request goes into the queue
pool.getConnection(function(err, connection) {
connection.end()
pool.end()
})

// Third connection request gets refused, since the queue is full
var thirdGetErr
pool.getConnection(function(err, connection) {
thirdGetErr = err
})

process.on('exit', function() {
assert.equal(thirdGetErr.message, 'Queue limit reached.')
})
16 changes: 16 additions & 0 deletions test/integration/pool/test-set-session-vars.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var common = require('../../common');
var assert = require('assert');
var Connection = require(common.lib + '/Connection');
var pool = common.createPool();

var wasSet = false;
pool.on('connection', function(err, connection) {
connection.query('SET SESSION sql_mode="STRICT_ALL_TABLES"');
wasSet = true;
})

pool.getConnection(function(err, connection) {
if (err) throw err;
assert.equal(wasSet, true);
pool.end();
});