Skip to main content
Version: Next

Mutex

Mutex

Async mutex implementation that provides mutual exclusion for shared resources. Ensures only one task can access a protected resource at a time.

Signature
class Mutex {
constructor(options: MutexOptions = {})
setStorage(storage: MutexStorage) => ;
getStorage() => MutexStorage;
acquire(key: string, timeout?: number, signal?: AbortSignal) => Promise<boolean>;
release(key: string) => Promise<void>;
isLocked(key: string) => Promise<boolean>;
withLock(key: string, fn: () => Promise<T> | T, timeout?: number, signal?: AbortSignal) => Promise<T>;
getConfig() => Omit<MutexOptions, 'storage'>;
}

constructor

method
(options: MutexOptions = {}) => Mutex

Creates a new mutex instance

setStorage

method
(storage: MutexStorage) =>

Sets the storage implementation for the mutex

getStorage

method

Gets the storage implementation for the mutex

acquire

method
(key: string, timeout?: number, signal?: AbortSignal) => Promise<boolean>

Acquires a lock for the given key

release

method
(key: string) => Promise<void>

Releases the lock for the given key

isLocked

method
(key: string) => Promise<boolean>

Checks if a lock is currently held for the given key

withLock

method
(key: string, fn: () => Promise<T> | T, timeout?: number, signal?: AbortSignal) => Promise<T>

Executes a function with exclusive access to the resource

getConfig

method
() => Omit<MutexOptions, 'storage'>

Gets the current configuration of the mutex