This commit is contained in:
Alan Mosko
2023-01-17 11:29:10 -03:00
parent c398aa3ca4
commit 05dd53e2ce
12 changed files with 217 additions and 106 deletions

View File

@@ -1,8 +1,20 @@
import logger from './logger'
const MUTEX_TIMEOUT_MS = 60_000
export const makeMutex = () => {
let task = Promise.resolve() as Promise<any>
let taskTimeout: NodeJS.Timeout | undefined
return {
mutex<T>(code: () => Promise<T> | T): Promise<T> {
task = (async() => {
const stack = new Error('mutex start').stack
let waitOver = false
taskTimeout = setTimeout(() => {
logger.warn({ stack, waitOver }, 'possible mutex deadlock')
}, MUTEX_TIMEOUT_MS)
// wait for the previous task to complete
// if there is an error, we swallow so as to not block the queue
try {