diff --git a/lock-client.js b/lock-client.js index 645e020..e593ad1 100644 --- a/lock-client.js +++ b/lock-client.js @@ -33,7 +33,7 @@ class LockClient extends EventEmitter { this.maxRetries = options.maxRetries || 5; this.socket = null; - this.requestId = null; + this.requests = new Map(); this.isLocked = false; this.lockType = null; this.timeoutHandle = null; @@ -74,12 +74,14 @@ class LockClient extends EventEmitter { await this.ensureConnected(); return new Promise((resolve, reject) => { - if (this.isLocked) { - reject(new Error('Lock already held')); - return; - } + // if (this.isLocked) { + // reject(new Error('Lock already held')); + // return; + // } this.requestId = uuidv4(); + this.requests.set(this.requestId, resolve) + this.lockType = type; // 发送锁请求 diff --git a/lock-client.namedpipe.js b/lock-client.namedpipe.js index 78d098a..04ca790 100644 --- a/lock-client.namedpipe.js +++ b/lock-client.namedpipe.js @@ -19,7 +19,7 @@ class NamedPipeRWLock extends LockClient { * @param {number} options.maxRetries - 最大重连次数,默认5 */ constructor(resource, options = {}) { - super(options); + super(resource, options); this.resource = resource; this.pipePath = options.connect.pipePath; } diff --git a/lock.js b/lock.js index abf3958..70ddf1a 100644 --- a/lock.js +++ b/lock.js @@ -82,7 +82,7 @@ class LockServer { handleReadLock(clientId, resource, requestId, lock) { if (!lock.writer) { // 可以立即获取读锁 - lock.readers.add(clientId); + lock.readers.add(`${clientId}_${requestId}`); this.sendToClient(clientId, { type: 'lockGranted', requestId, @@ -108,7 +108,7 @@ class LockServer { if (lock.readers.size === 0 && !lock.writer) { // 可以立即获取写锁 lock.writer = clientId; - this.sendToClient(clientId, { + this.sendToClient(`${clientId}_${requestId}`, { type: 'lockGranted', requestId, resource,