mirror of
https://github.com/renovatebot/renovate.git
synced 2026-10-24 14:34:21 +00:00
628f9a2835
fix(mutex): Support per-call-site acquire timeouts Closes #41210
27 lines
704 B
TypeScript
27 lines
704 B
TypeScript
import { Mutex, type MutexInterface, withTimeout } from 'async-mutex';
|
|
|
|
const DEFAULT_NAMESPACE = 'default';
|
|
const DEFAULT_TIMEOUT_MS = 2 * 60 * 1000;
|
|
let mutexes: Record<string, Record<string, Mutex>> = {};
|
|
|
|
export function initMutexes(): void {
|
|
mutexes = {};
|
|
}
|
|
|
|
export function getMutex(
|
|
key: string,
|
|
namespace: string = DEFAULT_NAMESPACE,
|
|
): Mutex {
|
|
mutexes[namespace] ??= {};
|
|
mutexes[namespace][key] ??= new Mutex();
|
|
return mutexes[namespace][key];
|
|
}
|
|
|
|
export function acquireLock(
|
|
key: string,
|
|
namespace: string = DEFAULT_NAMESPACE,
|
|
timeoutMs: number = DEFAULT_TIMEOUT_MS,
|
|
): Promise<MutexInterface.Releaser> {
|
|
return withTimeout(getMutex(key, namespace), timeoutMs).acquire();
|
|
}
|