npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

readwrite-lock

v1.0.5

Published

Read/Write lock on asynchronous code

Downloads

772

Readme

readwrite-lock

Read/Write locks on asynchronous code

Build Status Codacy Badge

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', () => {
    // Concurrency safe
    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', () => {
    // no other locks exist when this is running
    return concatHtml();
}).then(result => {
}).catch(err => {
});

lock.acquireRead('key', () => {
    // runs parallel with other read locks
    return readHtml();
}).then(result => {
}).catch(err => {
});

lock.acquireRead('key', () => {
    // runs parallel with other read locks
    return readHtml();
}).then(result => {
}).catch(err => {
});

Getting Started

var ReadwriteLock = require('readwrite-lock');
var lock = new ReadwriteLock();

/**
 * @param {String|Array} key 	resource key or keys to lock
 * @param {function} fn 	execute function
 * @param {Object} opts 	(optional) options
 */
lock.acquireRead(key, () => {
    // critical section
    // return value or promise
}, opts).then(() => {
    // continue execution outside critical section
    // NOTE: LOCK IS RELEASED AS SOON AS CRITICAL SECTION RETURNS
    //       there is no guaranteed order of this "then()" call
    //       compared to other recently released locks of same key
});

/**
 * @param {String|Array} key 	resource key or keys to lock
 * @param {function} fn 	execute function
 * @param {Object} opts 	(optional) options
 */
lock.acquireWrite(key, () => {
    // critical section
    // return value or promise
}, opts).then(() => {
    // continue execution outside critical section
    // NOTE: LOCK IS RELEASED AS SOON AS CRITICAL SECTION RETURNS
    //       there is no guaranteed order of this "then()" call
    //       compared to other recently released locks of same key
});

Error Handling

lock.acquireRead(key, () => {
    throw new Error('error');
}).catch(err => {
    console.log(err.message); // output: error
});


lock.acquireWrite(key, () => {
    throw new Error('error');
}).catch(err => {
    console.log(err.message); // output: error
});

Acquire multiple keys

lock.acquireRead([key1, key2], fn)
    .then(() => {
        // no longer in critical section
    })
    .catch(err => {
        console.log(err.message);
    });

lock.acquireWrite([key1, key2], fn)
    .then(() => {
        // no longer in critical section
    })
    .catch(err => {
        console.log(err.message);
    });

Options

// Specify timeout
var lock = new ReadwriteLock({timeout : 5000});
lock.acquireRead(key, () => {
    // critical section will never be entered if timeout occurs
}).catch(err => {
    // timed out error will be returned here if lock not acquired in given time
});
lock.acquireWrite(key, () => {
    // critical section will never be entered if timeout occurs
}).catch(err => {
    // timed out error will be returned here if lock not acquired in given time
});

// Set max pending tasks
var lock = new ReadwriteLock({maxPending : 1000});
lock.acquireRead(key, () => {
    // critical section will never be entered if pending limit reached
}).catch(err => {
    // too many pending tasks error will be returned here if lock not acquired in given time
});
lock.acquireWrite(key, () => {
    // critical section will never be entered if pending limit reached
}).catch(err => {
    // too many pending tasks error will be returned here if lock not acquired in given time
});

// Whether there is any running or pending async function
lock.isBusy();

// Use your own promise library instead of the global Promise variable
var lock = new ReadwriteLock({Promise : require('bluebird')}); // Bluebird
var lock = new ReadwriteLock({Promise : require('q').Promise}); // Q

Issues

See isse tracker.

License

MIT, see LICENSE