feat(lock-client): 重构锁请求管理机制以支持并发处理
将单一 requestId 字段替换为 Map 类型的 requests 字段,用于存储多个未完成的锁请求及其对应的 resolve 函数。注释掉原有 isLocked 检查逻辑,允许更灵活的锁控制流程。
fix(namedpipe): 修复父类构造函数参数传递错误
在 NamedPipeRWLock 构造函数中调用 super() 时补充缺失的 resource 参数,确保父类正确初始化。
feat(lock-server): 改进读锁客户端标识以增强唯一性
更新 readers 集合中的元素格式,从 clientId 变为 `${clientId}_${requestId}`,提高锁持有者标识的唯一性和可追踪性。
This commit is contained in:
parent
a9bdbf1d4a
commit
b38367f09f
|
|
@ -33,7 +33,7 @@ class LockClient extends EventEmitter {
|
||||||
this.maxRetries = options.maxRetries || 5;
|
this.maxRetries = options.maxRetries || 5;
|
||||||
|
|
||||||
this.socket = null;
|
this.socket = null;
|
||||||
this.requestId = null;
|
this.requests = new Map();
|
||||||
this.isLocked = false;
|
this.isLocked = false;
|
||||||
this.lockType = null;
|
this.lockType = null;
|
||||||
this.timeoutHandle = null;
|
this.timeoutHandle = null;
|
||||||
|
|
@ -74,12 +74,14 @@ class LockClient extends EventEmitter {
|
||||||
await this.ensureConnected();
|
await this.ensureConnected();
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (this.isLocked) {
|
// if (this.isLocked) {
|
||||||
reject(new Error('Lock already held'));
|
// reject(new Error('Lock already held'));
|
||||||
return;
|
// return;
|
||||||
}
|
// }
|
||||||
|
|
||||||
this.requestId = uuidv4();
|
this.requestId = uuidv4();
|
||||||
|
this.requests.set(this.requestId, resolve)
|
||||||
|
|
||||||
this.lockType = type;
|
this.lockType = type;
|
||||||
|
|
||||||
// 发送锁请求
|
// 发送锁请求
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,7 @@ class NamedPipeRWLock extends LockClient {
|
||||||
* @param {number} options.maxRetries - 最大重连次数,默认5
|
* @param {number} options.maxRetries - 最大重连次数,默认5
|
||||||
*/
|
*/
|
||||||
constructor(resource, options = {}) {
|
constructor(resource, options = {}) {
|
||||||
super(options);
|
super(resource, options);
|
||||||
this.resource = resource;
|
this.resource = resource;
|
||||||
this.pipePath = options.connect.pipePath;
|
this.pipePath = options.connect.pipePath;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
4
lock.js
4
lock.js
|
|
@ -82,7 +82,7 @@ class LockServer {
|
||||||
handleReadLock(clientId, resource, requestId, lock) {
|
handleReadLock(clientId, resource, requestId, lock) {
|
||||||
if (!lock.writer) {
|
if (!lock.writer) {
|
||||||
// 可以立即获取读锁
|
// 可以立即获取读锁
|
||||||
lock.readers.add(clientId);
|
lock.readers.add(`${clientId}_${requestId}`);
|
||||||
this.sendToClient(clientId, {
|
this.sendToClient(clientId, {
|
||||||
type: 'lockGranted',
|
type: 'lockGranted',
|
||||||
requestId,
|
requestId,
|
||||||
|
|
@ -108,7 +108,7 @@ class LockServer {
|
||||||
if (lock.readers.size === 0 && !lock.writer) {
|
if (lock.readers.size === 0 && !lock.writer) {
|
||||||
// 可以立即获取写锁
|
// 可以立即获取写锁
|
||||||
lock.writer = clientId;
|
lock.writer = clientId;
|
||||||
this.sendToClient(clientId, {
|
this.sendToClient(`${clientId}_${requestId}`, {
|
||||||
type: 'lockGranted',
|
type: 'lockGranted',
|
||||||
requestId,
|
requestId,
|
||||||
resource,
|
resource,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue