Skip to content

Extend Pool methods to enable create conns even if there are free ones available #1232

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 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
17 changes: 15 additions & 2 deletions lib/Pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,27 @@ Pool.prototype.getConnection = function (cb) {
}

var connection;
var pool = this;

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

return this.acquireConnection(connection, cb);
}

return this.createConnection(cb);

};

Pool.prototype.createConnection = function (cb) {

if (this._closed) {
return process.nextTick(function(){
return cb(new Error('Pool is closed.'));
});
}

var connection;
var pool = this;

if (this.config.connectionLimit === 0 || this._allConnections.length < this.config.connectionLimit) {
connection = new PoolConnection(this, { config: this.config.newConnectionConfig() });

Expand Down
25 changes: 25 additions & 0 deletions test/unit/pool/test-connection-create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var assert = require('assert');
var common = require('../../common');
var Connection = common.Connection;
var EventEmitter = require('events').EventEmitter;
var pool = common.createPool({
port: common.fakeServerPort
});
var PoolConnection = common.PoolConnection;

var server = common.createFakeServer();

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

pool.createConnection(function(err, connection) {
assert.ifError(err);

assert(connection instanceof PoolConnection);
assert(connection instanceof Connection);
assert(connection instanceof EventEmitter);

connection.destroy();
server.destroy();
});
});