Skip to content

Commit

Permalink
updated: pooling and better data selection
Browse files Browse the repository at this point in the history
  • Loading branch information
petersirka committed Nov 21, 2014
1 parent d0cbb6b commit 6074056
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 21 deletions.
32 changes: 19 additions & 13 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1260,6 +1260,12 @@ Database.prototype.execute = function(query, params, callback, custom) {

transaction.execute(query, params, function(err, result, meta, isSelect) {

if (isSelect) {
if (callback)
callback(err, result, meta, isSelect);
return;
}

if (err) {
transaction.rollback(function() {
doError(err, callback);
Expand Down Expand Up @@ -1397,10 +1403,8 @@ exports.pool = function(max, options, callback) {
throw err;

max--;

db.pool = max;
pool.db.push(db);
poolEvents(db, pool);

if (max <= 0) {
pool.isReady = true;
pool.check();
Expand Down Expand Up @@ -1469,16 +1473,18 @@ Pool.prototype.check = function() {
if (db.connection._isUsed)
continue;

db.removeAllListeners('detach');
poolEvents(db, self);

var cb = self.pending.shift();
if (cb) {
db.connection._isUsed = true;
cb(null, db);
}
if (!cb)
continue;

return self;
poolEvents(db, self);
db.connection._isUsed = true;
cb(null, db);

setImmediate(function() {
self.check();
});
break;
}

return self;
Expand Down Expand Up @@ -1909,8 +1915,8 @@ Connection.prototype.detach = function (callback, force) {

if (self.options.isPool && !force) {
self._isUsed = false;
// self._queue = [];
// self._pending = [];
self._queue = [];
self._pending = [];
self.db.emit('detach', true);
return;
}
Expand Down
62 changes: 54 additions & 8 deletions test/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ fb.attachOrCreate(config, function (err, db) {
var task = [];

task.push(test_create);
task.push(test_reconnect);
task.push(test_insert);
task.push(test_reconnect);
task.push(test_select_insert); // for inserted rows
task.push(test_update);
task.push(test_select_update); // for updated rows
Expand All @@ -61,7 +61,11 @@ fb.attachOrCreate(config, function (err, db) {
});

task.push(test_pooling);
task.async();
task.async(function() {
setTimeout(function() {
process.exit();
}, 2000);
});
});

function test_create(next) {
Expand Down Expand Up @@ -419,25 +423,67 @@ function test_pooling(next) {
// detach a current connection (socket is opened)
db.detach();
}, 1000);
next();
});
next();
});

query.push(function(next) {
pool.get(function(err, db) {
setTimeout(function() {
// detach a current connection (socket is still opened)
db.detach();
}, 1500);
next();
}, 2000);
});
next();
});

query.push(function(next) {
pool.get(function(err, db) {
next();
db.query('SELECT * FROM test WHERE id=1', function(err, results) {
setImmediate(function() {
assert.ok(db.pool === 1 && results.length === 1, 'pool selector 1');
db.detach();
});
});
});
next();
});

query.push(function(next) {
assert.ok(pool.pending.length === 1, name + ': pool pending');
next();
});

query.push(function(next) {
pool.get(function(err, db) {
db.query('SELECT * FROM test WHERE id=2', function(err, results) {
assert.ok(db.pool === 0 && results.length === 1, 'pool selector 2');
db.detach();
next();
});
});
});

query.push(function(next) {
pool.get(function(err, db) {
db.query('SELECT * FROM test WHERE id=1', function(err, results) {
setImmediate(function() {
assert.ok(db.pool === 1 && results.length === 1, 'pool selector 3');
db.detach();
});
next();
});
});
});

query.push(function(next) {
pool.get(function(err, db) {
db.query('SELECT * FROM test WHERE id=2', function(err, results) {
assert.ok(db.pool === 0 && results.length === 1, 'pool selector 4');
db.detach();
next();
});
});
assert.ok(pool.pending.length > 0, name + ': pool pending');
});

query.push(function(next) {
Expand All @@ -451,4 +497,4 @@ function test_pooling(next) {
setTimeout(function() {
query.async(next);
}, 1000);
}
}

0 comments on commit 6074056

Please sign in to comment.