Lock Qweue
Inter-process multi-resource locking queue. It has a server-client architecture. If you are looking for local (single-process) multi-resource locking, check out async-lock as it has been out there for longer.
Requirements
- Node.js at least v7.6.0 or higher for ES2015 and async function support
Install
npm i lock-qweue
List of classes
- Server (Lock qweue server)
- Client (Lock qweue client)
- LockSpace (Space where names of resources must be unique)
Server class
Functions
- constructor(options) → returns Server instance
- listen(port)
- close()
- io() → returns underlying Socket.io server
Examples
Start a server:
const Server = ; let server = port: 3000 token: 'secret' // (optional) Authentication token. maxPending: 100 // (optional) Max pending lock requests per namespace. logInfo: consolelog // (optional) Info logs function. logSuccess: consolelog // (optional) Success logs function. logError: consoleerror // (optional) Error logs function.;
or
const Server = ; let server = ;server;
Client class
Functions
- constructor(options) → returns Client instance
- async lockRequest(resources, options) → returns Request instance
- async tryLock(resources, options) → boolean (lock acquired flag)
- async release(resources, options) → boolean (all released resources were locked flag)
- async abort(requestId, options) → boolean (request id was found flag)
- async lock(resources, fn, options)
- io() → returns underlying Socket.io client
Examples
Connect a client:
const Client = ; let client = host: 'http://localhost:3000' namespace: 'name' // (optional) Namespace that will be used by default. Can be overridden with options. name: 'client1' // (optional) Client name. token: 'secret' // (optional) Authentication token. logError: consoleerror // (optional) Error logs function.;
Execute a function while resource lock is acquired:
await client
or
await clientpromise; // ... function here await client;
Try to lock resources:
let resourcesLocked = await client;
Abort a lock request:
let request = await client; // ... some code here await request;
Lock request with timeout:
let request = await client;await requestpromise; // If time runs out, this will throw an error.
LockSpace class
You can use this class if you want to lock resources locally (single-process).
Functions
- default(maxPending) → returns the default LockSpace instance
- tryLock(resources) → boolean (lock acquired flag)
- lock(resources, options) → string request id
- async lockAsync(resources, fn, timeout)
- abort(requestId) → boolean (request id was found flag)
- release(resources) → boolean (all released resources were locked flag)
- isEmpty() → boolean (true if the requests queue is empty and there are no locked resources)
Examples
Execute a function while resource lock is acquired:
const LockSpace = ; let space = ; await space;
or
const LockSpace = ; let space = ; await space; // Lock resources with an optional 1000ms timeout. // ... Your code here ...// NOTE: If needed wrap your code in a try catch statement to make sure the resources get released. // Do not forget to release the resources.space;
or
const LockSpace = ; let space = ; await space;
Development
Node.js libraries used
System that was used for development
- OS: Ubuntu 18.04
- Node.js: v8.11.3
Optional requirements for development
- docker
- npm package manager for JavaScript
- VS Code for Node.js development and debugging
Ideas for further development
- authentication to support multiple tokens and ip whitelisting
- optimizing the multi-resource locking queue algorithm
- support for REDIS