5

I'm somewhat new to the JavaScript/Typescript/Node/Express world, but from my research so far, there doesn't seem to be an 'accepted' way to lock critical sections of code in a Node/Express app. I've come across a couple of NPM packages (async-lock, await-lock, rwlock), but they all have a surprisingly low download/week count and seem like they are not particularly well maintained (in that their last publishes are old, or the official maintainer explicitly says that he's not actively maintaining it). None seem to have TypeScript definitions (at least as far as I can tell). And, most problematically, none seem to have much in terms of documentation (beyond a couple examples to show the common-case usage). I've seen a few questions here where people have written their own (often active polling) locks (which seems sub-optimal), or suggest using a DB for locking (which seems like a heavyweight solution to a lightweight problem). async-lock seems like the most popular of them (download-count-wise), but I'm a little wary of depending on something that the owner disclaims much responsibility for (and the Docs are quite thin).

My use-case seems pretty straight forward. I'm building a REST server, and the objects have some interdependencies. So, for instance, if someone is updating a FOO, and FOOs have references to BARs, then I'd want to lock my critical section on 'FOO' and 'BAR', then get the old FOO, any related BARs, validate the update, and write the new FOO back to the DB - after which I'd release the 'FOO' and 'BAR' locks.

So my question is this - for simple critical section locking in TypeScript (supporting multiple simultaneous locks/keys), what is the standard practice/API/package?

This is a single-server/single-DB application, so there's no need for distributed locks - just trying to deal with the fact that multiple requests are being handled simultaneously due to 'thread-switching' on asynchronous IO.

2 Answers 2

4

There's no such built-in functionality in Node.js. A user can use third-party libraries or develop own solution.

async-lock, the very first package you've listed, has considerable download stats, TypeScript definitions and is maintained.

Although a lot of small utility NPM packages may not be updated for years because they don't ever need that - and if they even do, they are simple enough to be forked, modified and optionally PRed.

This is a single-server/single-DB application, so there's no need for distributed locks - just trying to deal with the fact that multiple requests are being handled simultaneously due to 'thread-switching' on asynchronous IO.

Then it's likely a good use case for Node.js in-memory locks that don't need to be stored in database or filesystem.

Sign up to request clarification or add additional context in comments.

2 Comments

I was a little hesitant since the guy put the following Disclaimer up top: " I did not create this package, and I will not add any features to it myself. I was granted the ownership because it was no longer being maintained, and I volunteered to fix a bug." And there don't seem to be any Docs out there for it other than the brief examples on the NPM page. Are there any guides/tutorials/reference guides for it with a bit more information or fleshed-out examples?
The package seems perfectly legit to me. It required a few attention over the years (native Promise and some bugfixes) and it received it. As with other small packages, readme is the only documentation it requires, API is really simplistic (you can always check TS typings to get the gist of it, github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/… ), so is the lib itself github.com/rogierschouten/async-lock/blob/master/lib/index.js
0

See p-queue with concurrency=1

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.