Skip to content

Commit 4de8f04

Browse files
author
Ricky Ng-Adam
committed
count reuse of each connection as client.poolCount
1 parent 4c8f489 commit 4de8f04

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

Diff for: lib/pool.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,13 @@ var pools = {
4141
pool.destroy(client);
4242
}
4343
});
44-
44+
client.poolCount = 0;
4545
return cb(null, client);
4646
});
4747
},
4848
destroy: function(client) {
4949
client._destroying = true;
50+
client.poolCount = undefined;
5051
client.end();
5152
}
5253
});
@@ -66,6 +67,7 @@ var pools = {
6667
cb = domain.bind(cb);
6768
}
6869
if(err) return cb(err, null, function() {/*NOOP*/});
70+
client.poolCount++;
6971
cb(null, client, function(err) {
7072
if(err) {
7173
pool.destroy(client);

Diff for: test/unit/pool/basic-tests.js

+35
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,38 @@ test('fetching pool by object', function() {
177177
});
178178
assert.equal(p, p2);
179179
});
180+
181+
182+
test('pool#connect client.poolCount', function() {
183+
var p = pools.getOrCreate(poolId++);
184+
var tid;
185+
186+
setConnectTimeout = function() {
187+
tid = setTimeout(function() {
188+
throw new Error("Connection callback was never called");
189+
}, 100);
190+
}
191+
192+
setConnectTimeout();
193+
p.connect(function(err, client, done) {
194+
clearTimeout(tid);
195+
assert.equal(client.poolCount, 1,
196+
'after connect, poolCount should be 1');
197+
done();
198+
assert.equal(client.poolCount, 1,
199+
'after returning client to pool, poolCount should still be 1');
200+
setConnectTimeout();
201+
p.connect(function(err, client, done) {
202+
clearTimeout(tid);
203+
assert.equal(client.poolCount, 2,
204+
'after second connect, poolCount should be 2');
205+
done();
206+
setConnectTimeout();
207+
p.destroyAllNow(function() {
208+
clearTimeout(tid);
209+
assert.equal(client.poolCount, undefined,
210+
'after pool is destroyed, count should be undefined');
211+
});
212+
})
213+
});
214+
});

0 commit comments

Comments
 (0)