Skip to content

Add Pool option queueLimit #475

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 1 commit 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
6 changes: 5 additions & 1 deletion lib/Pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ Pool.prototype.getConnection = function(cb) {
}
});
} 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);
}
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.')
})