0.1.2 • Published 2 years ago

mutex-local v0.1.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

mutex-local

Mutex lock for multi-process synchronization.

npm i mutex-local

Usage

const mutexLocal = require('mutex-local')

const mutex = mutexLocal('./filename.lock')

await mutex.waitForLock()
console.log('here do a synced operation between multiple process')
await mutex.unlock()
const granted = await mutex.tryLock()
if (granted) {
  console.log('here do a synced operation between multiple process')
  await mutex.unlock()
}

If you exit the process with a locked mutex then it's automatically released.\ Still do unlock() when you can, to avoid file descriptor leaks.

API

const mutex = mutexLocal(filename)

Creates a mutex lock based on a filename, so multiple process can coordinate.

await mutex.waitForLock([options])

Default options:

{
  flags: 'a+',
  mode: 0o666,
  exclusive: true
}

const granted = await mutex.tryLock([options])

Options are the same as for waitForLock([options]).

await mutex.unlock()

Release the lock.

mutex.locked

Boolean that indicates if mutex is locked or not.

License

MIT