*Refactor mutex lock logic
This commit is contained in:
parent
5c02356595
commit
83b62bfdcb
|
@ -185,6 +185,8 @@ export class RPCServer extends EventEmitter {
|
||||||
this.cache.addItem(request, rpcResult);
|
this.cache.addItem(request, rpcResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.getRequestLock(request)?.release();
|
||||||
|
|
||||||
return rpcResult;
|
return rpcResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -225,16 +227,13 @@ export class RPCServer extends EventEmitter {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const reqId = RPCServer.hashQuery(request);
|
if (!this.getRequestLock(request)) {
|
||||||
|
this.createRequestLock(request);
|
||||||
let lock: Mutex = this.pendingRequests.get(reqId) as Mutex;
|
|
||||||
const lockExists = !!lock;
|
|
||||||
|
|
||||||
if (!lockExists) {
|
|
||||||
lock = new Mutex();
|
|
||||||
this.pendingRequests.set(reqId, lock);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const reqId = RPCServer.hashQuery(request);
|
||||||
|
const lock: Mutex = this.getRequestLock(request) as Mutex;
|
||||||
|
|
||||||
if (lock.isLocked()) {
|
if (lock.isLocked()) {
|
||||||
await lock.waitForUnlock();
|
await lock.waitForUnlock();
|
||||||
if (reqId in this._cache.data) {
|
if (reqId in this._cache.data) {
|
||||||
|
@ -244,4 +243,22 @@ export class RPCServer extends EventEmitter {
|
||||||
|
|
||||||
await lock.acquire();
|
await lock.acquire();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private getRequestLock(request: RPCRequest): Mutex | null {
|
||||||
|
const reqId = RPCServer.hashQuery(request);
|
||||||
|
|
||||||
|
let lock: Mutex = this.pendingRequests.get(reqId) as Mutex;
|
||||||
|
|
||||||
|
if (!lock) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return lock;
|
||||||
|
}
|
||||||
|
|
||||||
|
private createRequestLock(request: RPCRequest) {
|
||||||
|
const reqId = RPCServer.hashQuery(request);
|
||||||
|
|
||||||
|
this.pendingRequests.set(reqId, new Mutex());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue