Skip to content
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
168 changes: 86 additions & 82 deletions lib/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,104 +4,108 @@
*
***************************************/

function Pool(attach, max, options) {
this.attach = attach;
this.internaldb = []; // connection created by the pool (for destroy)
this.pooldb = []; // available connection in the pool
this.dbinuse = 0; // connection currently in use into the pool
this.max = max || 4;
this.pending = [];
this.options = options;
}

Pool.prototype.get = function(callback) {
var self = this;
self.pending.push(callback);
self.check();
return self;
};
class Pool {

Pool.prototype.check = function() {
constructor(attach, max, options) {
this.attach = attach;
this.internaldb = []; // connection created by the pool (for destroy)
this.pooldb = []; // available connection in the pool
this.dbinuse = 0; // connection currently in use into the pool
this.max = max || 4;
this.pending = [];
this.options = options;
}

var self = this;
if (self.dbinuse >= self.max)
get(callback) {
var self = this;
self.pending.push(callback);
self.check();
return self;
}

var cb = self.pending.shift();
if (!cb)
return self;
self.dbinuse++;
if (self.pooldb.length) {
cb(null, self.pooldb.shift());
} else {
this.attach(self.options, function (err, db) {
if (!err) {
self.internaldb.push(db);
db.on('detach', function () {
// also in pool (could be a twice call to detach)
if (self.pooldb.indexOf(db) !== -1 || self.internaldb.indexOf(db) === -1)
return;
// if not usable don't put in again in the pool and remove reference on it
if (db.connection._isClosed || db.connection._isDetach || db.connection._pooled === false)
self.internaldb.splice(self.internaldb.indexOf(db), 1);
else
self.pooldb.push(db);

if (db.connection._pooled)
self.dbinuse--;
self.check();
});
} else {
// attach fail so not in the pool
self.dbinuse--;
}
check() {

cb(err, db);
});
}
setImmediate(function() {
self.check();
});
var self = this;
if (self.dbinuse >= self.max)
return self;

return self;
};
var cb = self.pending.shift();
if (!cb)
return self;
self.dbinuse++;
if (self.pooldb.length) {
cb(null, self.pooldb.shift());
} else {
this.attach(self.options, function (err, db) {
if (!err) {
self.internaldb.push(db);
db.on('detach', function () {
// also in pool (could be a twice call to detach)
if (self.pooldb.indexOf(db) !== -1 || self.internaldb.indexOf(db) === -1)
return;
// if not usable don't put in again in the pool and remove reference on it
if (db.connection._isClosed || db.connection._isDetach || db.connection._pooled === false)
self.internaldb.splice(self.internaldb.indexOf(db), 1);
else
self.pooldb.push(db);

Pool.prototype.destroy = function(callback) {
var self = this;
if (db.connection._pooled)
self.dbinuse--;
self.check();
});
} else {
// attach fail so not in the pool
self.dbinuse--;
}

var connectionCount = this.internaldb.length;
cb(err, db);
});
}
setImmediate(function() {
self.check();
});

if (connectionCount === 0 && callback) {
callback();
return self;
}

function detachCallback(err) {
if (err) {
if (callback) {
callback(err);
}
return;
}
destroy(callback) {
var self = this;

var connectionCount = this.internaldb.length;

connectionCount--;
if (connectionCount === 0 && callback) {
callback();
}
}

this.internaldb.forEach(function(db) {
if (db.connection._pooled === false) {
detachCallback();
return;
}
// check if the db is not free into the pool otherwise user should manual detach it
var _db_in_pool = self.pooldb.indexOf(db);
if (_db_in_pool !== -1) {
self.pooldb.splice(_db_in_pool, 1);
db.connection._pooled = false;
db.detach(detachCallback);
function detachCallback(err) {
if (err) {
if (callback) {
callback(err);
}
return;
}

connectionCount--;
if (connectionCount === 0 && callback) {
callback();
}
}
});
};

this.internaldb.forEach(function(db) {
if (db.connection._pooled === false) {
detachCallback();
return;
}
// check if the db is not free into the pool otherwise user should manual detach it
var _db_in_pool = self.pooldb.indexOf(db);
if (_db_in_pool !== -1) {
self.pooldb.splice(_db_in_pool, 1);
db.connection._pooled = false;
db.detach(detachCallback);
}
});
}

}

module.exports = Pool;
Loading
Loading