local lock

This commit is contained in:
Ivan Socolsky 2015-04-08 10:21:01 -03:00
parent 0631083bae
commit c33e0b0de4
3 changed files with 44 additions and 32 deletions

View File

@ -1,33 +1,33 @@
var _ = require('lodash'); var _ = require('lodash');
var $ = require('preconditions').singleton(); var $ = require('preconditions').singleton();
var locks = {}; var locks = {};
var Lock = function() { function Lock() {
this.taken = false;
this.queue = [];
}; };
Lock.prototype.free = function() { Lock.prototype._runOne = function(token) {
if (this.queue.length > 0) { var self = this;
var f = this.queue.shift();
f(this); if (locks[token].length == 0) return;
} else {
this.taken = false; var task = locks[token][0];
}
task(null, function() {
locks[token].shift();
self._runOne(token);
});
}; };
Lock.get = function(key, callback) { Lock.prototype.locked = function(token, wait, max, task) {
if (_.isUndefined(locks[key])) { if (_.isUndefined(locks[token])) {
locks[key] = new Lock(); locks[token] = [];
} }
var lock = locks[key];
if (lock.taken) { locks[token].push(task);
lock.queue.push(callback);
} else { if (locks[token].length == 1) {
lock.taken = true; this._runOne(token);
callback(lock);
} }
}; };

View File

@ -3,15 +3,18 @@ var _ = require('lodash');
var LocalLock = require('./locallock'); var LocalLock = require('./locallock');
var RemoteLock = require('locker'); var RemoteLock = require('locker');
function Lock(opts) {}; function Lock(opts) {
this.lock = new LocalLock();
};
Lock.prototype.runLocked = function(token, cb, task) { Lock.prototype.runLocked = function(token, cb, task) {
$.shouldBeDefined(token); $.shouldBeDefined(token);
LocalLock.get(token, function(lock) { this.lock.locked(token, 2 * 1000, 10 * 60 * 1000, function(err, release) {
if (err) return cb(new Error('Wallet is locked'));
var _cb = function() { var _cb = function() {
cb.apply(null, arguments); cb.apply(null, arguments);
lock.free(); release();
}; };
task(_cb); task(_cb);
}); });

View File

@ -6,18 +6,25 @@ var sinon = require('sinon');
var should = chai.should(); var should = chai.should();
var Lock = require('../lib/locallock'); var Lock = require('../lib/locallock');
describe('Local lock', function() {
describe('Local locks', function() {
var lock;
beforeEach(function() {
lock = new Lock();
});
it('should lock tasks using the same token', function(done) { it('should lock tasks using the same token', function(done) {
var a = false, var a = false,
b = false; b = false;
Lock.get('123', function(lock) { lock.locked('123', 0, 0, function(err, release) {
should.not.exist(err);
a = true; a = true;
setTimeout(function() { setTimeout(function() {
lock.free(); release();
}, 5); }, 5);
Lock.get('123', function(lock) { lock.locked('123', 0, 0, function(err, release) {
should.not.exist(err);
b = true; b = true;
lock.free(); release();
}); });
}); });
setTimeout(function() { setTimeout(function() {
@ -32,14 +39,16 @@ describe('Local lock', function() {
}); });
it('should not lock tasks using different tokens', function(done) { it('should not lock tasks using different tokens', function(done) {
var i = 0; var i = 0;
Lock.get('123', function(lock) { lock.locked('123', 0, 0, function(err, release) {
should.not.exist(err);
i++; i++;
setTimeout(function() { setTimeout(function() {
lock.free(); release();
}, 5); }, 5);
Lock.get('456', function(lock) { lock.locked('456', 0, 0, function(err, release) {
should.not.exist(err);
i++; i++;
lock.free(); release();
}); });
}); });
setTimeout(function() { setTimeout(function() {