rwlockfile
lockfile utility with reader/writers
Last updated 7 years ago by dickeyxxx .
ISC · Repository · Bugs · Original npm · Tarball · package.json
$ npm install rwlockfile 
SYNC missed versions from official npm registry.

rwlockfile

code style: prettier Greenkeeper badge codecov CircleCI Build status npm npm npm

node utility for read/write lockfiles

This is the only node package as of this writing I'm aware of that allows you to have read/write lockfiles. If you're looking for a simpler solution, check out proper-lockfile. Use this package if you need read/write lock support.

This follows the standard Readers-Writers Lock design pattern. Any number of readers are allowed at one time. 1 writer is allowed at one time iff there are no current readers.

Usage

const {RWLockfile} = require('rwlockfile')

// 'myfile' is the path to a file to use as the base for the lockfile
// it will add '.lock' to the end for the actual lockfile, so in this case 'myfile.lock'
let lock = new RWLockfile('myfile', {
  // how long to wait until timeout. Default: 30000
  timeout: 30000,
  // mininum time to wait between checking for locks
  // automatically adds some noise and duplicates this number each check
  retryInterval: 10,
})

// add a reader async or synchronously. If the count is >= 1 it creates a read lock (see note below)
await lock.add('read')
lock.addSync('read')

// remove a reader async or synchronously. If the count == 0 it creates removes the read lock
await lock.remove('read')
lock.removeSync('read')

// add a writer async or synchronously
await lock.add('write')
lock.addSync('write')

Behavior Note

The use of .add() and .remove() may be a bit misleading but allow me to attempt to explain what it means. It's designed to make it easy to add try/finally steps around locking. Each instance of RWLockfile has a count of readers and writers. The file itself has it's own count of readers and writers. When you increment the count, what you're doing is adding to the count of just that instance.

In other words, you can do lock.add('write') multiple times on the same instance. That instance will create the write lock if the count is greater than 1 but no other instances will be allowed to increase the count above 0.

Why? Because this way you can have functions in your code like this:

async function doAThing() {
  await lock.add('write')
  try {
    // do something while locked
    doAnotherThing()
  } finally {
    await lock.remove('write')
  }
}

async function doAnotherThing() {
  await lock.add('write')
  try {
    // do something else while locked
  } finally {
    await lock.remove('write')
  }
}

This will only create a single write lock which will be removed after doAThing() is done. This way you can call doAnotherThing() and it ensures it has a lock if it doesn't exist, but will only remove the write lock if nothing it's using also has a write lock.

I've found this behavior to be perfect for working with, but this sort of nesting lock logic is a little difficult to explain.

Current Tags

  • 2.0.25                                ...           latest (7 years ago)

51 Versions

  • 2.0.25                                ...           7 years ago
  • 2.0.24                                ...           7 years ago
  • 2.0.23                                ...           7 years ago
  • 2.0.22                                ...           7 years ago
  • 2.0.21                                ...           7 years ago
  • 2.0.20                                ...           7 years ago
  • 2.0.19                                ...           7 years ago
  • 2.0.18                                ...           7 years ago
  • 2.0.17                                ...           7 years ago
  • 2.0.16                                ...           7 years ago
  • 2.0.15                                ...           7 years ago
  • 2.0.14                                ...           7 years ago
  • 2.0.13                                ...           7 years ago
  • 2.0.12                                ...           7 years ago
  • 2.0.11                                ...           7 years ago
  • 2.0.10                                ...           7 years ago
  • 2.0.9                                ...           7 years ago
  • 2.0.8                                ...           7 years ago
  • 2.0.7                                ...           7 years ago
  • 2.0.6                                ...           7 years ago
  • 2.0.5                                ...           7 years ago
  • 2.0.4                                ...           7 years ago
  • 2.0.3                                ...           7 years ago
  • 2.0.2                                ...           7 years ago
  • 2.0.1                                ...           7 years ago
  • 2.0.0                                ...           7 years ago
  • 1.4.12                                ...           7 years ago
  • 1.4.11                                ...           7 years ago
  • 1.4.10                                ...           7 years ago
  • 1.4.9                                ...           7 years ago
  • 1.4.8                                ...           7 years ago
  • 1.4.7                                ...           7 years ago
  • 1.4.6                                ...           7 years ago
  • 1.4.5                                ...           7 years ago
  • 1.4.4                                ...           7 years ago
  • 1.4.3                                ...           7 years ago
  • 1.4.2                                ...           8 years ago
  • 1.4.1                                ...           8 years ago
  • 1.4.0                                ...           8 years ago
  • 1.3.9                                ...           8 years ago
  • 1.3.8                                ...           8 years ago
  • 1.3.7                                ...           8 years ago
  • 1.3.6                                ...           8 years ago
  • 1.3.5                                ...           8 years ago
  • 1.3.4                                ...           8 years ago
  • 1.3.3                                ...           8 years ago
  • 1.3.2                                ...           8 years ago
  • 1.3.1                                ...           8 years ago
  • 1.1.0                                ...           8 years ago
  • 1.0.1                                ...           8 years ago
  • 1.0.0                                ...           8 years ago
Downloads
Total 1
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (3)
Dev Dependencies (14)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE