https://github.com/mimamch/mutex
NodeJS Transaction Locking Mutex Example
https://github.com/mimamch/mutex
js-mutex lock locking mutex mutex-lock mutex-synchronisation node transaction
Last synced: 8 days ago
JSON representation
NodeJS Transaction Locking Mutex Example
- Host: GitHub
- URL: https://github.com/mimamch/mutex
- Owner: mimamch
- Created: 2023-10-11T05:06:41.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-10-11T05:06:45.000Z (over 2 years ago)
- Last Synced: 2024-12-18T16:34:56.477Z (over 1 year ago)
- Topics: js-mutex, lock, locking, mutex, mutex-lock, mutex-synchronisation, node, transaction
- Language: TypeScript
- Homepage:
- Size: 3.91 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# NodeJS Mutex Example
This is a simple example of how to use mutexes in NodeJS for transaction.
## How it's works?
First you call this function, it will add lock data into transaction mutex's map.
```
startTransaction('user123', {createdAt: new Date()});
```
Then on twice you call the method again, its will be throw an error because the user123 transaction is already running.
When you call
```
endTransaction('user123')
```
It will remove the lock data from transaction mutex's map. And you can call the startTransaction again.
## How about startTransaction function called in same time?
Javascript is running by single thread. So it will be executed one by one.
## How about deadlock?
Before its throw an error, the startTransaction will check when the last transaction is started. And if last transaction started on more than 60 seconds ago, it will not throw an error and the transaction will be started again.
## Usage Example
```javascript
const transactionExample = async () => {
const userId = "123";
try {
await startTransaction(userId, {
createdAt: new Date(),
});
// Simulate a long running transaction
await new Promise((resolve) => setTimeout(resolve, 1000));
userBalance = userBalance + 1000;
} catch (error) {
console.log(error);
} finally {
await endTransaction(userId);
}
};
```