0
0
Fork 0
mirror of https://github.com/renovatebot/renovate.git synced 2025-01-12 14:19:01 +00:00
renovatebot_renovate/lib/util/mutex.ts
Sebastian Poxhofer f042ae4c9a
feat(cache): add a mutex utility and prevent parallel processing with the same cache key (#30815)
Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
2024-08-16 17:14:48 +00:00

21 lines
659 B
TypeScript

import { Mutex, type MutexInterface, withTimeout } from 'async-mutex';
const DEFAULT_NAMESPACE = 'default';
const mutexes: Record<string, Record<string, MutexInterface>> = {};
export function getMutex(
key: string,
namespace: string = DEFAULT_NAMESPACE,
): MutexInterface {
mutexes[namespace] ??= {};
// create a new mutex if it doesn't exist with a timeout of 2 minutes
mutexes[namespace][key] ??= withTimeout(new Mutex(), 1000 * 60 * 2);
return mutexes[namespace][key];
}
export function acquireLock(
key: string,
namespace: string = DEFAULT_NAMESPACE,
): Promise<MutexInterface.Releaser> {
return getMutex(key, namespace).acquire();
}