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

redis-lock

v1.0.0

Published

A locking primitive using redis.

Downloads

200,673

Readme

redis-lock

Build Status

Implements a locking primitive using redis in node.js.

Fully non-blocking and asynchronous, and uses the algorithm described in the redis docs.

Useful for concurrency control. For example, when updating a database record you might want to ensure that no other part of your code is updating the same record at that time.

Used heavily at errorception.

Requires v4 of node-redis For previous versions use v0.1.4

Example

const client = require("redis").createClient(),
	lock = require("redis-lock")(client);

await client.connect(); // node-redis v4 needs to connect before sending commands

const done = await lock("myLock")
// No one else will be able to get a lock on 'myLock' until you call done()
await done();

Slightly more descriptive example:

const client = require("redis").createClient(),
	lock = require("redis-lock")(client);

await client.connect();

const done1 = await lock("myLock")
// Simulate a 1 second long operation
setTimeout(done1, 1000);

const done2 = await lock("myLock")
// Even though this function has been scheduled at the same time
// as the function above, this callback will not be executed till
// the function above has called done(). Hence, this will have to
// wait for at least 1 second.
await done2();

Installation

$ npm install redis-lock

Usage

redis-lock is really simple to use - It's just a function!

Initialization

To initialize redis-lock, simply call it by passing in a redis client instance, created by calling .createClient() on the excellent node-redis. This is taken in as a parameter because you might want to configure the client to suit your environment (host, port, etc.), and to enable you to reuse the client from your app if you want to.

You can also provide a second (optional) parameter: retryDelay. If due to any reason a lock couldn't be acquired, lock acquisition is retried after waiting for a little bit of time. retryDelay lets you control this delay time. Default: 50ms.

const lock = require("redis-lock")(require("redis").createClient(), 10);

This will return a function called (say) lock, described below:

lock(lockName, [timeout = 5000]): done

  • lockName: Any name for a lock. Must follow redis's key naming rules. Make this as granular as you can. For example, to get a lock when editing record 1 in the database, call the lock record1 rather than database, so that other records in the database can be modified even as you are holding this lock.
  • timeout: (Optional) The maximum time (in ms) to hold the lock for. If this time is exceeded, the lock is automatically released to prevent deadlocks. Default: 5000 ms (5 seconds).

Returns a done async function which releases the lock (or do nothing if timeout has already released it)

Full example, with console.log calls to illustrate the flow:

const client = require("redis").createClient(),
	lock = require("redis-lock")(client);

await client.connect();

console.log("Asking for lock");
const done = await lock("myLock")
console.log("Lock acquired");
await someTask() // Some async task
console.log("Releasing lock now");
await done()
console.log("Lock has been released, and is available for others to use");

Details

  • It's guaranteed that only one function will be called at a time for the same lock.
  • This module doesn't block the event loop. All operations are completely asynchronous and non-blocking.
  • If two functions happen to ask for a lock simultaneously, the execution of the second function is deferred until the first function has released its lock or has timed out.
  • It's not possible for two functions to acquire the same lock at any point in time, except if the timeout is breached.
  • If the timeout is breached, the lock is released, and the next function coming along and asking for a lock acquires the lock.
  • Since it's asynchronous, different functions could be holding different locks simultaneously. This is awesome!
  • If redis is down for any reason, none of the functions are given locks, and none of the locks are released. The code will keep polling to check if redis is available again to acquire the lock.

License

(The MIT License)

Copyright (c) 2012 Rakesh Pai [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.