travix-lock-manager

0.4.1 • Public • Published

travix-lock-manager

NPM

Build Status Code Climate Test Coverage

In-memory lock manager for NodeJs helping to serialize access to a set of hierarchically organized resources with various levels of exclusivity.

Supports transactional semantics and can be easily persisted/distributed. Does not detect deadlocks.

See more information about lock managers on Wikipedia.

Installation

Install it via npm:

$ npm install --save travix-lock-manager

Usage

const { CR, CW, EX, NL, PR, PW } = LockManager;
 
const manager = new LockManager({
  onacquire: locks => console.log(
    'Acquired:',
    ...locks.map(lock => lock.toString())
  ),
  onrelease: locks => console.log(
    'Released:',
    ...locks.map(lock => lock.toString())
  )
});
 
await manager.acquire('resource');
// Acquired: Lock of "resource" for "Exclusive"
await manager.release('resource');
// Released: Lock of "resource" for "Exclusive"
 
await manager.acquire('resource', PW, 'owner1');
// Acquired: Lock of "resource" by "owner1" for "Protected Write"
await manager.acquire('resource', PW, 'owner2');
// Error: Some requested locks cannot be acquired
await manager.acquire('resource', PW | CR, 'owner2');
// Acquired: Lock of "resource" by "owner2" for "Concurrent Read"
await manager.acquire(['resource/a', 'resource/b'], NL, 'owner3');
// Acquired: Lock of "resource/a" by "owner3" for "Null" Lock of "resource/b" by "owner3" for "Null"
await manager.release(null, NL, 'owner3');
// Released: Lock of "resource/a" by "owner3" for "Null" Lock of "resource/b" by "owner3" for "Null"
 
manager.keys;
// [ '', 'resource' ]
manager.locks;
/*
[
  Lock { key: '', mode: 4, owner: 'owner1' },
  Lock { key: '', mode: 2, owner: 'owner2' },
  Lock { key: 'resource', mode: 16, owner: 'owner1' },
  Lock { key: 'resource', mode: 2, owner: 'owner2' }
]
*/
 
manager.describe(CW);
// 'Concurrent Write'
 
manager.select('resource');
/*
Set {
  Lock { key: 'resource', mode: 16, owner: 'owner1' },
  Lock { key: 'resource', mode: 2, owner: 'owner2' }
}
*/

Scripts

To run linting of source code and unit tests:

$ npm run lint

To run unit tests and generate test coverage report:

$ npm run test

Use NodeJs version 7.5 or higher to run this script because it relies on native support of async/await.

To generate documentation files:

$ npm run doc

Use NodeJs version 7.5 or higher to run this script because it relies on native support of async/await.

License

MIT © Travix International

Package Sidebar

Install

npm i travix-lock-manager

Weekly Downloads

1

Version

0.4.1

License

MIT

Last publish

Collaborators

  • vladen