mutual-exclusion (mutex)

Mutual Exclusion (mutex) object for JavaScript.
Motivation
Promised based mutex allows to sequentially perform the same asynchronous operation. A typical use case example is checking if a resource exists and reading/ creating resource as an atomic asynchronous operation.
Suppose that you have a HTTP service that upon request downloads and serves an image. A naive implementation might look something like this:
router.use('/images/:uid', async (incomingMessage, serverResponse) => {
const uid = incomingMessage.params.uid;
const temporaryDownloadPath = path.resolve(
downloadDirectoryPath,
uid,
);
const temporaryFileExists = await fs.pathExists(temporaryDownloadPath);
if (!temporaryFileExists) {
try {
await pipeline(
storage.createReadStream('images/' + uid),
fs.createWriteStream(temporaryDownloadPath),
);
} catch (error) {
await fs.remove(temporaryDownloadPath);
throw error;
}
}
fs
.createReadStream(temporaryDownloadPath)
.pipe(serverResponse);
});
In the above example, if two requests are made at near the same time, then both of them will identify that temporaryFileExists is false and at least one of them will fail. Mutex solves this problem by limiting concurrent execution of several asynchronous operations (see Image server example).
API
import {
createMutex,
HoldTimeoutError,
WaitTimeoutError,,
} from 'mutual-exclusion';
import type {
LockConfigurationInputType,
MutexConfigurationInputType,
MutexType,
} from 'mutual-exclusion';
HoldTimeoutError;
WaitTimeoutError;
const mutex: MutexType = createMutex(mutexConfigurationInput: MutexConfigurationInputType);
(async () => {
await mutex.lock(async () => {
mutex.isLocked();
});
mutex.isLocked();
})()
Configuration
MutexConfigurationInputType (at the mutex-level) and LockConfigurationInputType (at the individual lock-level) can be used to configure the scope and timeouts.
type MutexConfigurationInputType = {|
+holdTimeout?: number,
+key?: string,
+waitTimeout?: number,
|};
type LockConfigurationInputType = {|
+holdTimeout?: number,
+key?: string,
+waitTimeout?: number,
|};
Usage examples
Image server example
Motivation section of the documentation demonstrates a flawed implementation of an image proxy server. That same service can utilise Mutex to solve the illustrated problem:
router.use('/images/:uid', async (incomingMessage, serverResponse) => {
const uid = incomingMessage.params.uid;
const temporaryDownloadPath = path.resolve(
downloadDirectoryPath,
uid,
);
await mutex.lock(async () => {
const temporaryFileExists = await fs.pathExists(temporaryDownloadPath);
if (!temporaryFileExists) {
try {
await pipeline(
storage.createReadStream('images/' + uid),
fs.createWriteStream(temporaryDownloadPath),
);
} catch (error) {
await fs.remove(temporaryDownloadPath);
throw error;
}
}
}, {
holdTimeout: 30000,
key: uid,
waitTimeout: 5000,
});
fs
.createReadStream(temporaryDownloadPath)
.pipe(serverResponse);
});
Related libraries
- await-mutex – Similar implementation to mutual-exclusion, but without separation by
key and timeouts.