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

distributedlock

v1.0.0

Published

A distributed lock algorithm using redis, see http://redis.io/topics/distlock. Uses a Redis instance to allow distributed lock. This allows a certain process to make use of a resource one at a time.

Downloads

4

Readme

Uses a Redis instance to allow distributed lock. This allows a certain process to make use of a resource one at a time.

  • Allows a single redis instance that uses redlock algorithm to be use
  • Or in case of failover support a Redis sentinel is used to manage multiple redis instance that sa master/slave roles.

Requirements

All Redis Instance

  • Make sure the dump.rdb can be written. For single instances this way redis does not fail when writing to disk. This enables the slave redis replication.

Multiple Redis Instance

  • Redis instances/sentinel are should be running for the lock dependency to run, since this module will try to connect to the sentinel to get the master redis instance.

Redis config (redis.conf)

All Redis Instance
notify-keyspace-events KEA
port <port #>
slave-read-only yes
slave-priority <priority number> //the lower to more in priority getting to be master

Slave Redis Instance
slaveof <masterip|machine name> <masterport>

Sentinel Config
sentinel monitor master <redis master instance> <redis master port> 1
sentinel down-after-milliseconds master 2000
sentinel failover-timeout master 3000

Redis Readings

Redis replication http://redis.io/topics/replication Redis Sentinel http://redis.io/topics/sentinel

Usage

//Single Redis

var dlock = require('distributedLock');
var lock = new dlock({key: 'adm', host:'localhost', port:6379});

lock.on("acquired", function (key) {
  console.log('*** Startup ***');
});


//Multiple Redis with fail ove support
var dlock = require('distributedLock');
// take note that instead of declaring host we declared the sentinel values. master/slaves are automatically discover when this modules connects to a redis sentinel
var lock = new dlock({key: 'adm', sentinel: {host: 'localhost', port: 26379}});

lock.on("acquired", function (key) {
  console.log('*** Startup ***');
});

Scenarios

See test scenarios ./test/

API

Events

acquired

acquired is emitted when lock has been successfully acquired.

var lock = new DistributedLock();
lock.on('acquired', function(key){
  //do my stuff
});

released

released is emitted when a lock has been obtained and has been released.

var lock = new DistributedLock();
lock.on('released', function(key){
  //do my stuff on lock released
});

failed

failed is emitted when a lock is NOT obtained after trying to acquire lock

var lock = new DistributedLock();
lock.on('failed', function(key){
  // lock is not aquired do some processing
});

listening

listening is emitted when lock is not acquired but is waiting for the lock has been released.

var lock = new DistributedLock();
lock.on('listening', function(key){
  // lock is not aquired do some process while it start to listen for the lock
});

Methods

release

When called,

  • it releases the current lock and ends the session.
  • if the lock was not acquired and is currently in monitoring mode, this ends the current monitoring and redis session.
var lock = new DistributedLock();
lock.release();

Notes

  • When multiple redis is used for the setup to support failover: ** It is best o give the TTL a higher value since it takes a while before the the responsibility is switched to another redis. This is due to the Redis Sentinel timing. On my test its ideal to have TTL: 10000 and renewTTL of 5000. You can play around with the timing but this works best so that the sentinel timing will not produce a timing problem. ** The timing greatly increases the switching timing when renewing/reaquiring the locks. ** when a master redis instance goes down, another redis instance is promoted to be a master. The instance that went down can be brought up but will have the role of a slave

Future Plans

  • Add more test to mimic shutting down servers and putting them back up again and make sure the the lock is managed correctly.

License

MIT