*Refactor mutex lock logic

This commit is contained in:
Derrick Hammer 2022-11-26 17:53:16 -05:00
parent 5c02356595
commit 83b62bfdcb
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 25 additions and 8 deletions

View File

@ -185,6 +185,8 @@ export class RPCServer extends EventEmitter {
this.cache.addItem(request, rpcResult);
}
this.getRequestLock(request)?.release();
return rpcResult;
}
@ -225,16 +227,13 @@ export class RPCServer extends EventEmitter {
return;
}
const reqId = RPCServer.hashQuery(request);
let lock: Mutex = this.pendingRequests.get(reqId) as Mutex;
const lockExists = !!lock;
if (!lockExists) {
lock = new Mutex();
this.pendingRequests.set(reqId, lock);
if (!this.getRequestLock(request)) {
this.createRequestLock(request);
}
const reqId = RPCServer.hashQuery(request);
const lock: Mutex = this.getRequestLock(request) as Mutex;
if (lock.isLocked()) {
await lock.waitForUnlock();
if (reqId in this._cache.data) {
@ -244,4 +243,22 @@ export class RPCServer extends EventEmitter {
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());
}
}