readwrite-lock
Read/Write locks on asynchronous code

Installation
npm install readwrite-lock
Readwrite lock rules:
- There may be one or more readers at a time
- There may be only one writer at a time
- Attempts to acquire read lock are queued as long as a write lock is taken
- Attempts to acquire write lock are queued as long as at least one read lock is taken
Features
- Uses ES6 promises
- Individual or an array of lock keys supported
- Timeout supported
- Pending task limit supported
Why you need locking on single threaded nodejs?
Nodejs is single threaded, and the code execution is never get interrupted inside an event loop, so locking is unnecessary? This is true ONLY IF your critical section can be executed inside a single event loop. However, if you have any async code inside your critical section (it can be simply triggered by any I/O operation, or timer), your critical logic will across multiple event loops, therefore it's not concurrency safe!
Consider the following code
redis.get('key', function(err, value){
redis.set('key', value * 2);
});
The above code simply multiply a redis key by 2.
However, if two users run concurrency, the execution order may like this
user1: redis.get('key') -> 1
user2: redis.get('key') -> 1
user1: redis.set('key', 1 x 2) -> 2
user2: redis.set('key', 1 x 2) -> 2
Obviously it's not what you expected
With readwriteLock, you can easily write your async critical section
lock.acquireWrite('key', () => {
return new Promise((resolve, reject) => {
redis.get('key', (err, value) => {
redis.set('key', value * 2, (err, value) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
});
}).then((result) => {
}).catch((err) => {
});
Why read/write locking?
Read locks run concurrently, while write locks run exclusively. This is useful when working with multiple async IO calls to modify a resource. Consider the below example of how read write locks can help organize your async nodejs app. This is of course could be fixed using better coding rather than locks, it is only used as an example of what can happen when using asynchronous IO in NodeJS.
function concatHtml() {
return new Promise((resolve, reject) => {
htmlDownload("https://www.google.com", googleHtml => {
fs.writeFile('concat_html.txt', googleHtml, () => {
htmlDownload("https://www.github.com", githubHtml => {
fs.appendFile('concat_html.txt', githubHtml, () => resolve());
});
});
});
});
}
function readHtml() {
return new Promise((resolve, reject) => {
fs.readFile('concat_html.txt', result => resolve(result))
});
}
user1: concatHtml()
user2: readHtml() -> only googleHtml found in file
user3: readHtml() -> googleHtml + githubHtml found in file
With readwriteLock, you can make sure that read locks can run concurrently as long as no writes are queued up. Likewise, writes block all reads until they are completed.
lock.acquireWrite('key', () => {
return concatHtml();
}).then(result => {
}).catch(err => {
});
lock.acquireRead('key', () => {
return readHtml();
}).then(result => {
}).catch(err => {
});
lock.acquireRead('key', () => {
return readHtml();
}).then(result => {
}).catch(err => {
});
Getting Started
var ReadwriteLock = require('readwrite-lock');
var lock = new ReadwriteLock();
lock.acquireRead(key, () => {
}, opts).then(() => {
});
lock.acquireWrite(key, () => {
}, opts).then(() => {
});
Error Handling
lock.acquireRead(key, () => {
throw new Error('error');
}).catch(err => {
console.log(err.message);
});
lock.acquireWrite(key, () => {
throw new Error('error');
}).catch(err => {
console.log(err.message);
});
Acquire multiple keys
lock.acquireRead([key1, key2], fn)
.then(() => {
})
.catch(err => {
console.log(err.message);
});
lock.acquireWrite([key1, key2], fn)
.then(() => {
})
.catch(err => {
console.log(err.message);
});
Options
var lock = new ReadwriteLock({timeout : 5000});
lock.acquireRead(key, () => {
}).catch(err => {
});
lock.acquireWrite(key, () => {
}).catch(err => {
});
var lock = new ReadwriteLock({maxPending : 1000});
lock.acquireRead(key, () => {
}).catch(err => {
});
lock.acquireWrite(key, () => {
}).catch(err => {
});
lock.isBusy();
var lock = new ReadwriteLock({Promise : require('bluebird')});
var lock = new ReadwriteLock({Promise : require('q').Promise});
Issues
See isse tracker.
License
MIT, see LICENSE