Skip to content

Commit 1047aeb

Browse files
committed
Merge pull request #571 from letsface/ended-event-on-pool-drained
emit event 'ended' on pool drained
2 parents 65d177e + 0882c8d commit 1047aeb

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

lib/index.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,23 @@ util.inherits(PG, EventEmitter);
1919

2020
PG.prototype.end = function() {
2121
var self = this;
22-
Object.keys(self.pools.all).forEach(function(key) {
22+
var keys = Object.keys(self.pools.all);
23+
var count = keys.length;
24+
keys.forEach(function(key) {
2325
var pool = self.pools.all[key];
2426
delete self.pools.all[key];
2527
pool.drain(function() {
26-
pool.destroyAllNow();
28+
pool.destroyAllNow(function() {
29+
count--;
30+
if(count === 0) {
31+
self.emit('end');
32+
}
33+
});
2734
});
2835
});
2936
};
3037

38+
3139
PG.prototype.connect = function(config, callback) {
3240
if(typeof config == "function") {
3341
callback = config;

test/integration/connection-pool/ending-pool-tests.js

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ var called = false;
44
test('disconnects', function() {
55
var sink = new helper.Sink(4, function() {
66
called = true;
7+
var eventSink = new helper.Sink(1, function() {});
8+
helper.pg.on('end', function() {
9+
eventSink.add();
10+
});
11+
712
//this should exit the process, killing each connection pool
813
helper.pg.end();
914
});

0 commit comments

Comments
 (0)