locker: more refactoring.
This commit is contained in:
parent
15350ead4f
commit
f5a8cb3ec0
@ -73,7 +73,7 @@ function Chain(options) {
|
|||||||
this.total = 0;
|
this.total = 0;
|
||||||
this.currentBlock = null;
|
this.currentBlock = null;
|
||||||
this.orphanLimit = options.orphanLimit || (20 << 20);
|
this.orphanLimit = options.orphanLimit || (20 << 20);
|
||||||
this.locker = new bcoin.locker(this, this.add);
|
this.locker = new bcoin.locker(true);
|
||||||
this.invalid = {};
|
this.invalid = {};
|
||||||
this.bestHeight = -1;
|
this.bestHeight = -1;
|
||||||
this.tip = null;
|
this.tip = null;
|
||||||
|
|||||||
@ -40,7 +40,7 @@ function RPC(node) {
|
|||||||
this.walletdb = node.walletdb;
|
this.walletdb = node.walletdb;
|
||||||
this.logger = node.logger;
|
this.logger = node.logger;
|
||||||
|
|
||||||
this.locker = new bcoin.locker(this);
|
this.locker = new bcoin.locker();
|
||||||
|
|
||||||
this.feeRate = null;
|
this.feeRate = null;
|
||||||
this.mining = false;
|
this.mining = false;
|
||||||
|
|||||||
@ -75,7 +75,7 @@ function Mempool(options) {
|
|||||||
this.logger = options.logger || this.chain.logger;
|
this.logger = options.logger || this.chain.logger;
|
||||||
this.loaded = false;
|
this.loaded = false;
|
||||||
|
|
||||||
this.locker = new bcoin.locker(this, this.addTX);
|
this.locker = new bcoin.locker(true);
|
||||||
|
|
||||||
this.size = 0;
|
this.size = 0;
|
||||||
this.totalOrphans = 0;
|
this.totalOrphans = 0;
|
||||||
|
|||||||
@ -82,7 +82,7 @@ function Peer(pool, addr, socket) {
|
|||||||
this.chain = this.pool.chain;
|
this.chain = this.pool.chain;
|
||||||
this.mempool = this.pool.mempool;
|
this.mempool = this.pool.mempool;
|
||||||
this.network = this.chain.network;
|
this.network = this.chain.network;
|
||||||
this.locker = new bcoin.locker(this);
|
this.locker = new bcoin.locker();
|
||||||
this.version = null;
|
this.version = null;
|
||||||
this.destroyed = false;
|
this.destroyed = false;
|
||||||
this.ack = false;
|
this.ack = false;
|
||||||
|
|||||||
@ -103,7 +103,7 @@ function Pool(options) {
|
|||||||
this.connected = false;
|
this.connected = false;
|
||||||
this.uid = 0;
|
this.uid = 0;
|
||||||
this.createServer = null;
|
this.createServer = null;
|
||||||
this.locker = new bcoin.locker(this);
|
this.locker = new bcoin.locker();
|
||||||
this.proxyServer = null;
|
this.proxyServer = null;
|
||||||
this.auth = null;
|
this.auth = null;
|
||||||
this.identityKey = null;
|
this.identityKey = null;
|
||||||
|
|||||||
@ -15,23 +15,22 @@ var assert = utils.assert;
|
|||||||
* Represents a mutex lock for locking asynchronous object methods.
|
* Represents a mutex lock for locking asynchronous object methods.
|
||||||
* @exports Locker
|
* @exports Locker
|
||||||
* @constructor
|
* @constructor
|
||||||
* @param {Function} parent - Parent object.
|
|
||||||
* @param {Function?} add - `add` method (whichever method is queuing data).
|
* @param {Function?} add - `add` method (whichever method is queuing data).
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function Locker(parent, add) {
|
function Locker(add) {
|
||||||
if (!(this instanceof Locker))
|
if (!(this instanceof Locker))
|
||||||
return Locker.create(parent, add);
|
return Locker.create(add);
|
||||||
|
|
||||||
EventEmitter.call(this);
|
EventEmitter.call(this);
|
||||||
|
|
||||||
this.parent = parent;
|
this.add = add === true;
|
||||||
|
|
||||||
this.jobs = [];
|
this.jobs = [];
|
||||||
this.busy = false;
|
this.busy = false;
|
||||||
|
|
||||||
this.pending = [];
|
this.pending = [];
|
||||||
this.pendingMap = {};
|
this.pendingMap = {};
|
||||||
this.add = add;
|
|
||||||
|
|
||||||
this.unlocker = this.unlock.bind(this);
|
this.unlocker = this.unlock.bind(this);
|
||||||
}
|
}
|
||||||
@ -40,15 +39,14 @@ utils.inherits(Locker, EventEmitter);
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a closure scoped locker.
|
* Create a closure scoped locker.
|
||||||
* @param {Function} parent - Parent object.
|
|
||||||
* @param {Function?} add
|
* @param {Function?} add
|
||||||
* @returns {Function} Lock method.
|
* @returns {Function} Lock method.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Locker.create = function create(parent, add) {
|
Locker.create = function create(add) {
|
||||||
var locker = new Locker(parent, add);
|
var locker = new Locker(add);
|
||||||
return function lock(func, args, force) {
|
return function lock(arg1, arg2) {
|
||||||
return locker.lock(func, args, force);
|
return locker.lock(arg1, arg2);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -138,9 +136,10 @@ Locker.prototype.unlock = function unlock() {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
Locker.prototype.destroy = function destroy() {
|
Locker.prototype.destroy = function destroy() {
|
||||||
|
this.jobs.length = 0;
|
||||||
|
this.busy = false;
|
||||||
this.pending.length = 0;
|
this.pending.length = 0;
|
||||||
this.pendingMap = {};
|
this.pendingMap = {};
|
||||||
this.jobs.length = 0;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -166,28 +165,25 @@ Locker.prototype.onDrain = function onDrain() {
|
|||||||
* Locks methods according to passed-in key.
|
* Locks methods according to passed-in key.
|
||||||
* @exports MappedLock
|
* @exports MappedLock
|
||||||
* @constructor
|
* @constructor
|
||||||
* @param {Function} parent - Parent object.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function MappedLock(parent) {
|
function MappedLock() {
|
||||||
if (!(this instanceof MappedLock))
|
if (!(this instanceof MappedLock))
|
||||||
return MappedLock.create(parent);
|
return MappedLock.create();
|
||||||
|
|
||||||
this.parent = parent;
|
|
||||||
this.jobs = {};
|
this.jobs = {};
|
||||||
this.busy = {};
|
this.busy = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a closure scoped locker.
|
* Create a closure scoped locker.
|
||||||
* @param {Function} parent - Parent object.
|
|
||||||
* @returns {Function} Lock method.
|
* @returns {Function} Lock method.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
MappedLock.create = function create(parent) {
|
MappedLock.create = function create() {
|
||||||
var locker = new MappedLock(parent);
|
var locker = new MappedLock();
|
||||||
return function lock(key, func, args, force) {
|
return function lock(key, force) {
|
||||||
return locker.lock(key, func, args, 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
|
* Expose
|
||||||
*/
|
*/
|
||||||
|
|
||||||
exports = Locker;
|
exports = Locker;
|
||||||
exports.mapped = MappedLock;
|
exports.mapped = MappedLock;
|
||||||
|
|
||||||
module.exports = exports;
|
module.exports = exports;
|
||||||
|
|||||||
@ -211,7 +211,7 @@ function TXDB(wallet) {
|
|||||||
this.options = wallet.db.options;
|
this.options = wallet.db.options;
|
||||||
this.locked = {};
|
this.locked = {};
|
||||||
|
|
||||||
this.locker = new bcoin.locker(this);
|
this.locker = new bcoin.locker();
|
||||||
this.coinCache = new bcoin.lru(10000);
|
this.coinCache = new bcoin.lru(10000);
|
||||||
|
|
||||||
this.current = null;
|
this.current = null;
|
||||||
|
|||||||
@ -56,8 +56,8 @@ function Wallet(db, options) {
|
|||||||
this.db = db;
|
this.db = db;
|
||||||
this.network = db.network;
|
this.network = db.network;
|
||||||
this.logger = db.logger;
|
this.logger = db.logger;
|
||||||
this.writeLock = new bcoin.locker(this);
|
this.writeLock = new bcoin.locker();
|
||||||
this.fundLock = new bcoin.locker(this);
|
this.fundLock = new bcoin.locker();
|
||||||
|
|
||||||
this.wid = 0;
|
this.wid = 0;
|
||||||
this.id = null;
|
this.id = null;
|
||||||
|
|||||||
@ -135,9 +135,9 @@ function WalletDB(options) {
|
|||||||
|
|
||||||
// We need one read lock for `get` and `create`.
|
// We need one read lock for `get` and `create`.
|
||||||
// It will hold locks specific to wallet ids.
|
// It will hold locks specific to wallet ids.
|
||||||
this.readLock = new bcoin.locker.mapped(this);
|
this.readLock = new bcoin.locker.mapped();
|
||||||
this.writeLock = new bcoin.locker(this);
|
this.writeLock = new bcoin.locker();
|
||||||
this.txLock = new bcoin.locker(this);
|
this.txLock = new bcoin.locker();
|
||||||
|
|
||||||
this.walletCache = new bcoin.lru(10000);
|
this.walletCache = new bcoin.lru(10000);
|
||||||
this.accountCache = new bcoin.lru(10000);
|
this.accountCache = new bcoin.lru(10000);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user