import { ILockService } from "@connext/types"; import { Mutex, MutexInterface } from "async-mutex"; import { IO_SEND_AND_WAIT_TIMEOUT } from "../../constants"; type InternalLock = { lock: Mutex; releaser: MutexInterface.Releaser; timer: NodeJS.Timeout; }; export class MemoryLockService implements ILockService { public readonly locks: Map = new Map(); async acquireLock(lockName: string): Promise { let lock: Mutex; if (!this.locks.has(lockName)) { lock = new Mutex(); } else { lock = this.locks.get(lockName)!.lock; } const releaser = await lock.acquire(); const timer = setTimeout(() => this.releaseLock(lockName), IO_SEND_AND_WAIT_TIMEOUT + 1_000); this.locks.set(lockName, { lock, releaser, timer }); } async releaseLock(lockName: string, lockValue?: string): Promise { const lock = this.locks.get(lockName); clearTimeout(lock!.timer); return lock!.releaser(); } }