Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚡️ Deduplicate Backend.getOps() calls #680

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion lib/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ function Backend(options) {
function(error, context) {
logger.error(error);
};

var backend = this;
this._dbGetOps = util.deduplicateRequests(function() {
backend.db.getOps.apply(backend.db, arguments);
});
}
module.exports = Backend;
emitter.mixin(Backend);
Expand Down Expand Up @@ -324,7 +329,7 @@ Backend.prototype._getSanitizedOps = function(agent, projection, collection, id,
var backend = this;
if (!opsOptions) opsOptions = {};
if (agent) opsOptions.agentCustom = agent.custom;
backend.db.getOps(collection, id, from, to, opsOptions, function(err, ops) {
this._dbGetOps(collection, id, from, to, opsOptions, function(err, ops) {
if (err) return callback(err);
backend._sanitizeOps(agent, projection, collection, id, ops, function(err) {
if (err) return callback(err);
Expand Down
26 changes: 26 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,32 @@ exports.clone = function(obj) {
return (obj === undefined) ? undefined : JSON.parse(JSON.stringify(obj));
};

exports.deduplicateRequests = function(fn) {
var callbacksByArgs = {};
return function() {
var args = Array.prototype.slice.call(arguments);
var callback = args.pop();
var argString = JSON.stringify(args);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agent.custom could in theory contain non-JSON-serializable objects, and also in practice could result in calls from different clients not being deduped.

We'll have to think on this a bit more - Alec suggested possibly moving it to sharedb-mongo instead


var callbacks = exports.digOrCreate(callbacksByArgs, argString, function() {
return [];
});
callbacks.push(callback);

if (callbacks.length > 1) return;

args.push(function() {
while (callbacks.length) {
var cb = callbacks.shift();
cb.apply(null, arguments);
}
delete callbacksByArgs[argString];
});

fn.apply(null, args);
};
};

var objectProtoPropNames = Object.create(null);
Object.getOwnPropertyNames(Object.prototype).forEach(function(prop) {
if (prop !== '__proto__') {
Expand Down
17 changes: 17 additions & 0 deletions test/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,23 @@ describe('Backend', function() {
done();
});
});

it('deduplicates concurrent requests', function(done) {
var getOps = sinon.spy(backend.db, 'getOps');
var count = 0;
var callback = function(error, ops) {
if (error) return done(error);
expect(ops).to.have.length(2);
expect(ops[0].create.data).to.eql({title: '1984'});
expect(ops[1].op).to.eql([{p: ['author'], oi: 'George Orwell'}]);
count++;
expect(getOps).to.have.been.calledOnce;
if (count === 2) done();
};

backend.getOps(agent, 'books', '1984', 0, null, callback);
backend.getOps(agent, 'books', '1984', 0, null, callback);
});
});

describe('getOpsBulk', function() {
Expand Down
Loading