locker: more refactoring.

This commit is contained in:
Christopher Jeffrey 2016-09-30 11:41:52 -07:00
parent 15350ead4f
commit f5a8cb3ec0
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
9 changed files with 37 additions and 31 deletions

View File

@ -73,7 +73,7 @@ function Chain(options) {
this.total = 0;
this.currentBlock = null;
this.orphanLimit = options.orphanLimit || (20 << 20);
this.locker = new bcoin.locker(this, this.add);
this.locker = new bcoin.locker(true);
this.invalid = {};
this.bestHeight = -1;
this.tip = null;

View File

@ -40,7 +40,7 @@ function RPC(node) {
this.walletdb = node.walletdb;
this.logger = node.logger;
this.locker = new bcoin.locker(this);
this.locker = new bcoin.locker();
this.feeRate = null;
this.mining = false;

View File

@ -75,7 +75,7 @@ function Mempool(options) {
this.logger = options.logger || this.chain.logger;
this.loaded = false;
this.locker = new bcoin.locker(this, this.addTX);
this.locker = new bcoin.locker(true);
this.size = 0;
this.totalOrphans = 0;

View File

@ -82,7 +82,7 @@ function Peer(pool, addr, socket) {
this.chain = this.pool.chain;
this.mempool = this.pool.mempool;
this.network = this.chain.network;
this.locker = new bcoin.locker(this);
this.locker = new bcoin.locker();
this.version = null;
this.destroyed = false;
this.ack = false;

View File

@ -103,7 +103,7 @@ function Pool(options) {
this.connected = false;
this.uid = 0;
this.createServer = null;
this.locker = new bcoin.locker(this);
this.locker = new bcoin.locker();
this.proxyServer = null;
this.auth = null;
this.identityKey = null;

View File

@ -15,23 +15,22 @@ var assert = utils.assert;
* Represents a mutex lock for locking asynchronous object methods.
* @exports Locker
* @constructor
* @param {Function} parent - Parent object.
* @param {Function?} add - `add` method (whichever method is queuing data).
*/
function Locker(parent, add) {
function Locker(add) {
if (!(this instanceof Locker))
return Locker.create(parent, add);
return Locker.create(add);
EventEmitter.call(this);
this.parent = parent;
this.add = add === true;
this.jobs = [];
this.busy = false;
this.pending = [];
this.pendingMap = {};
this.add = add;
this.unlocker = this.unlock.bind(this);
}
@ -40,15 +39,14 @@ utils.inherits(Locker, EventEmitter);
/**
* Create a closure scoped locker.
* @param {Function} parent - Parent object.
* @param {Function?} add
* @returns {Function} Lock method.
*/
Locker.create = function create(parent, add) {
var locker = new Locker(parent, add);
return function lock(func, args, force) {
return locker.lock(func, args, force);
Locker.create = function create(add) {
var locker = new Locker(add);
return function lock(arg1, arg2) {
return locker.lock(arg1, arg2);
};
};
@ -138,9 +136,10 @@ Locker.prototype.unlock = function unlock() {
*/
Locker.prototype.destroy = function destroy() {
this.jobs.length = 0;
this.busy = false;
this.pending.length = 0;
this.pendingMap = {};
this.jobs.length = 0;
};
/**
@ -166,28 +165,25 @@ Locker.prototype.onDrain = function onDrain() {
* Locks methods according to passed-in key.
* @exports MappedLock
* @constructor
* @param {Function} parent - Parent object.
*/
function MappedLock(parent) {
function MappedLock() {
if (!(this instanceof MappedLock))
return MappedLock.create(parent);
return MappedLock.create();
this.parent = parent;
this.jobs = {};
this.busy = {};
}
/**
* Create a closure scoped locker.
* @param {Function} parent - Parent object.
* @returns {Function} Lock method.
*/
MappedLock.create = function create(parent) {
var locker = new MappedLock(parent);
return function lock(key, func, args, force) {
return locker.lock(key, func, args, force);
MappedLock.create = function create() {
var locker = new MappedLock();
return function lock(key, force) {
return locker.lock(key, force);
};
};
@ -255,10 +251,20 @@ MappedLock.prototype.unlock = function unlock(key) {
};
};
/**
* Destroy the locker. Purge all pending calls.
*/
MappedLock.prototype.destroy = function destroy() {
this.jobs = {};
this.busy = {};
};
/*
* Expose
*/
exports = Locker;
exports.mapped = MappedLock;
module.exports = exports;

View File

@ -211,7 +211,7 @@ function TXDB(wallet) {
this.options = wallet.db.options;
this.locked = {};
this.locker = new bcoin.locker(this);
this.locker = new bcoin.locker();
this.coinCache = new bcoin.lru(10000);
this.current = null;

View File

@ -56,8 +56,8 @@ function Wallet(db, options) {
this.db = db;
this.network = db.network;
this.logger = db.logger;
this.writeLock = new bcoin.locker(this);
this.fundLock = new bcoin.locker(this);
this.writeLock = new bcoin.locker();
this.fundLock = new bcoin.locker();
this.wid = 0;
this.id = null;

View File

@ -135,9 +135,9 @@ function WalletDB(options) {
// We need one read lock for `get` and `create`.
// It will hold locks specific to wallet ids.
this.readLock = new bcoin.locker.mapped(this);
this.writeLock = new bcoin.locker(this);
this.txLock = new bcoin.locker(this);
this.readLock = new bcoin.locker.mapped();
this.writeLock = new bcoin.locker();
this.txLock = new bcoin.locker();
this.walletCache = new bcoin.lru(10000);
this.accountCache = new bcoin.lru(10000);