make kestrel a tak server, so that it can send and receive pois as cots data
Some checks failed
ci/woodpecker/pr/pr Pipeline failed
Some checks failed
ci/woodpecker/pr/pr Pipeline failed
This commit is contained in:
39
server/utils/asyncLock.js
Normal file
39
server/utils/asyncLock.js
Normal file
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* Async lock utility - Promise-based mutex per key.
|
||||
* Ensures only one async operation executes per key at a time.
|
||||
*/
|
||||
|
||||
const locks = new Map()
|
||||
|
||||
/**
|
||||
* Acquire a lock for a key and execute callback.
|
||||
* Only one callback per key executes at a time.
|
||||
* @param {string} key - Lock key
|
||||
* @param {Function} callback - Async function to execute
|
||||
* @returns {Promise<any>} Result of callback
|
||||
*/
|
||||
export async function acquire(key, callback) {
|
||||
const lockKey = String(key)
|
||||
let queue = locks.get(lockKey)
|
||||
|
||||
if (!queue) {
|
||||
queue = Promise.resolve()
|
||||
locks.set(lockKey, queue)
|
||||
}
|
||||
|
||||
const next = queue.then(() => callback()).finally(() => {
|
||||
if (locks.get(lockKey) === next) {
|
||||
locks.delete(lockKey)
|
||||
}
|
||||
})
|
||||
|
||||
locks.set(lockKey, next)
|
||||
return next
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all locks (for testing).
|
||||
*/
|
||||
export function clearLocks() {
|
||||
locks.clear()
|
||||
}
|
||||
Reference in New Issue
Block a user