Skip to content

Commit 621fc0d

Browse files
committed
Add Pool option queueLimit which enforces a maximum size of a Pool's queued connection requests. Fixes issue #475.
1 parent cdd0846 commit 621fc0d

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

Readme.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,9 @@ addition to those options pools accept a few extras:
255255
pool will immediately call back with an error. (Default: `true`)
256256
* `connectionLimit`: The maximum number of connections to create at once.
257257
(Default: `10`)
258+
* `queueLimit`: The maximum number of connection requests the pool will queue
259+
before returning an error from `getConnection`. If set to `0`, there is no
260+
limit to the number of queued connection requests. (Default: `0`)
258261

259262
## Switching users / altering connection state
260263

lib/Pool.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ Pool.prototype.getConnection = function (cb) {
4747
return cb(new Error('No connections available.'));
4848
}
4949

50+
if (this.config.queueLimit && this._connectionQueue.length >= this.config.queueLimit) {
51+
return cb(new Error('Queue limit reached.'));
52+
}
53+
5054
this._connectionQueue.push(cb);
5155
};
5256

lib/PoolConfig.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,7 @@ function PoolConfig(options) {
1111
this.connectionLimit = (options.connectionLimit === undefined)
1212
? 10
1313
: Number(options.connectionLimit);
14+
this.queueLimit = (options.queueLimit === undefined)
15+
? 0
16+
: Number(options.queueLimit);
1417
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
var common = require('../../common');
2+
var assert = require('assert');
3+
var pool = common.createPool({
4+
connectionLimit : 1,
5+
queueLimit : 1,
6+
waitForConnections : true
7+
});
8+
9+
// First connection we get right away
10+
pool.getConnection(function(err, connection) {
11+
connection.end()
12+
})
13+
14+
// Second connection request goes into the queue
15+
pool.getConnection(function(err, connection) {
16+
connection.end()
17+
pool.end()
18+
})
19+
20+
// Third connection request gets refused, since the queue is full
21+
var thirdGetErr
22+
pool.getConnection(function(err, connection) {
23+
thirdGetErr = err
24+
})
25+
26+
process.on('exit', function() {
27+
assert.equal(thirdGetErr.message, 'Queue limit reached.')
28+
})

0 commit comments

Comments
 (0)