Skip to content

Commit

Permalink
perf(user): save user.save till very end of ops queue
Browse files Browse the repository at this point in the history
  • Loading branch information
lefnire committed Feb 6, 2014
1 parent bf5e901 commit 4c3c4b2
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions src/controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -473,28 +473,33 @@ api.batchUpdate = function(req, res, next) {
var oldSend = res.send;
var oldJson = res.json;

var callOp = function(_req, cb) {
res.send = res.json = function(code, data) {
if (_.isNumber(code) && code >= 500)
return cb(code+": "+ (data.message ? data.message : data.err ? data.err : JSON.stringify(data)));
return cb();
};
api[_req.op](_req, res);
};
// Stash user.save, we'll queue the save op till the end (so we don't overload the server)
var oldSave = user.save;
user.save = function(cb){cb(null,user)}

res.locals.ops = [];
// Setup the array of functions we're going to call in parallel with async
var ops = _.transform(req.body, function(result, _req) {
if (!_.isEmpty(_req)) {
result.push(function(cb) {
res.locals.ops.push(_req);
callOp(_req, cb);
});
}
res.locals.ops = [];
var ops = _.transform(req.body, function(m,_req){
if (_.isEmpty(_req)) return;
m.push(function() {
var cb = arguments[arguments.length-1];
res.locals.ops.push(_req);
res.send = res.json = function(code, data) {
if (_.isNumber(code) && code >= 500)
return cb(code+": "+ (data.message ? data.message : data.err ? data.err : JSON.stringify(data)));
return cb();
};
api[_req.op](_req, res);
});
})
// Finally, save user at the end
.concat(function(){
user.save = oldSave;
user.save(arguments[arguments.length-1]);
});

// call all the operations, then return the user object to the requester
async.series(ops, function(err) {
async.waterfall(ops, function(err,user) {
res.json = oldJson;
res.send = oldSend;
if (err) return next(err);
Expand Down

0 comments on commit 4c3c4b2

Please sign in to comment.