lockwrap
v0.1.2
Published
Add concurrency control in a pinch using transparent wrappers.
Downloads
1
Maintainers
Readme
lockwrap
Funnel a function's invocations into a queue or guard them with a nonblocking mutex.
queued
Forces a function's calls into a queue, causing them to execute sequentially.
Example:
let serialFetch = queued(function(release, a) {
api.fetch(a, function(result) {
process(result); // Do something with result
release(); // Release exclusive lock
}, release); // We provide `release` as an error callback to `api.fetch`
// so that the lock will be released even if the find
// fails
});
Adding a timeout
You can enforce a timeout on exclusivity by adding { timeout: <milliseconds> }
as an argument to queued()
.
Example:
let serialFn = queued(function(release) {
doLengthyWork();
release();
}, { timeout: 100 });
Here, the release
callback will be automatically called after 100 milliseconds,
allowing the next call to be executed even if the current one is still in progress.
passThrough
Wraps the provided function in a limited-lifespan, nonblocking mutex, preventing
reentry into the function until the provided release
callback is called.
While an execution of the function is in progress, all would-be concurrent calls are thrown out.
passThrough
accepts a timeout as well via { timeout: <milliseconds> }
.
TODO
- switch to Promises and return function's results / error
- support custom release/timeout functions
- optionally raise an exception when lock cannot be obtained