npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

lock-qweue

v1.0.2

Published

Inter-process multi-resource locking queue.

Downloads

21

Readme

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 = require('lock-qweue/server');

let server = new Server({
	port: 3000,
	token: 'secret', // (optional) Authentication token.
	maxPending: 100, // (optional) Max pending lock requests per namespace.
	logInfo: console.log, // (optional) Info logs function.
	logSuccess: console.log, // (optional) Success logs function.
	logError: console.error, // (optional) Error logs function.
});

or

const Server = require('lock-qweue/server');

let server = new Server();
server.listen(3000);

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 = require('lock-qweue/client');

let client = new 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: console.error, // (optional) Error logs function.
});

Execute a function while resource lock is acquired:

await client.lock(['resource A', 'resource B'], async () => {
	// ... function here
})

or

await client.lockRequest(['resource A', 'resource B']).promise;

// ... function here

await client.release(['resource A', 'resource B']);

Try to lock resources:

let resourcesLocked = await client.tryLock(['resource A', 'resource B']);

Abort a lock request:

let request = await client.lockRequest(['resource A', 'resource B'], {
	namespace: 'name', // (optional) Override the default client namespace.
});

// ... some code here

await request.abort();

Lock request with timeout:

let request = await client.lockRequest(['resource A', 'resource B'], {
	timeout: 1000, // (optional) Timeout in milliseconds.
});
await request.promise; // 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 = require('lock-qweue/lock-space');

let space = new LockSpace();

await space.lockAsync(['resource A', 'resource B'], async () => {
	// ... function here
});

or

const LockSpace = require('lock-qweue/lock-space');

let space = new LockSpace();

await space.waitLock(['resource A', 'resource B'], 1000); // 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.release(['resource A', 'resource B']);

or

const LockSpace = require('lock-qweue/lock-space');

let space = new LockSpace();

await space.lock(['resource A', 'resource B'], {
	resolve: () => {
		// ... function here

		space.release(['resource A', 'resource B']);
	},
	reject: (error) => {
		space.release(['resource A', 'resource B']);
		console.error(error.message);
	},
	timeout: 1000, // (optional) Timeout in milliseconds.
});

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