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

simple-redis-mutex

v1.4.0

Published

Mutex lock implemented using redis

Downloads

16,414

Readme

Simple Redis Mutex

Implements mutex lock using redis as described in redis docs. The term simple is opposed to the more complex Redlock, that was also proposed by Redis in their docs for use in case of distributed redis instances.

This implementation of redis lock introduces some fine tuning features to the lock such as lock expire time, and acquire retry time, and acquire timeout (all described below).

Also acquiring the lock in a FIFO manner is supported by just providing a boolean option fifo: true, this will make the lock behave like a mutex queue and allow only one instance to acquire the lock at a time, and once the lock is released the first one to wait for it is the first one to acquire it and so on.

Install

Install the package using npm.

npm i simple-redis-mutex

Examples

const { lock } = require('simple-redis-mutex');
const Redis = require('ioredis');

// Connect to redis using ioredis
redis = new Redis(process.env.REDIS_URI);

async function someFunction() {
  // Acquire the lock, by passing redis client and the resource name (all settings are optional)
  const release = await lock(redis, 'resource-name');

  // Do some operations that require mutex lock
  await doSomeCriticalOperations();

  // Release the lock
  await release();
}

Usage

To acquire the lock you just call the lock function exported from the package, and pass to it ioredis client, and the resource name for the lock. You can also pass any optional options to fine-tune the lock as needed (see API below).

API

The package exports one named function lock, that acquires the lock and returns another function that releases the lock. The API for the lock function is as follows ...

lock(
  client,
  lockName,
  { retryTimeMillis = 100, timeoutMillis, failAfterMillis, fifo }
): Promise<Function>
  • client <ioredis client>: ioredis client.
  • lockName <String>: This is the name of the lock, and this is what distinguishes one lock from another, so that the part that needs mutual exclusion would always require a lock with the same name to be acquired by any process that attempts to enter that part. The key in redis database will be derived from this name.
  • retryTimeMillis <Number>: (default 100) This defines how much should a process wait before trying to acquire the same lock again, provided time is milliseconds, this time cannot be null.
  • timeoutMillis <Number>: (default null) This defines the expiry time of the lock after it's acquired, so after that expiry time another process can acquire the lock even if the current holder did not release it, time provided is in milliseconds, null timeout value means that the lock will never expire.
  • failAfterMillis <Number>: (default null) This defines the maximum time a process should wait for the lock until it can acquire it, when this time has passes and the process has not acquired the lock yet, the function will throw an Error saying that the lock could not be acquired in the given time, the provided time is in milliseconds, null value means that the function will not fail until it has acquired the lock.
  • fifo <Boolean>: (default false) If this is set the waiting instances will be acquire the lock in a FIFO manner, i.e. the first one to wait will be the first one to acquire the lock once it's released and so on.
  • Return type <Promise<Function>>: The unlock function, that is an async function, and should be called to release the lock.

Notes

  • This package has Peer Dependency on ioredis.
  • It's taken into account the case that process A acquires the lock, then it expires, then process B acquires the lock. When process A try to release the lock, it will not be released, as it's now acquired by B.
  • The same lock can be acquired with different options each time, so one time it can have an expiry time, and the next acquire it can lock indefinitely, the same with all the other options, although this behavior is not encouraged as it can be hard to debug.