diff --git a/app.js b/app.js index 45682c2..971bedb 100644 --- a/app.js +++ b/app.js @@ -11,6 +11,7 @@ const app = express(); const handlers = {}; const middlewares = {}; const dbPools = {}; +const lockHash = {}; let sessionClass = null; let useMiddlewares = []; @@ -204,6 +205,24 @@ registerHandler('Ops', 'selfChecking', async (req, rsp) => { } }); +async function lock(key) { + if (key in lockHash) { + lockHash[key] += 1; + } else { + lockHash[key] = 1; + } + const oldTimes = lockHash[key]; + while (lockHash[key] == oldTimes) { + await sleep(0.05); + } +} + +function unLock(key) { + if (key in lockHash) { + lockHash[key] -= 1; + } +} + exports.init = init; exports.listen = listen; exports.get = get; @@ -219,3 +238,5 @@ exports.getPid = getPid; exports.getNodeId = getNodeId; exports.getInstanceId = getInstanceId; exports.sleep = sleep; +exports.lock = lock; +exports.unLock = unLock;