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

clusterix

v5.0.0

Published

Node module for cluster node management

Downloads

102

Readme

clusterix

Build Status npm package Coverage Status

Node module for cluster node management

Introduction

Inspired by metroplex, this module is meant to be used standalone in order to detect nodes entering and leaving a cluster. This is useful, for example, if you are running socket.io in a cluster, and you want to be able to detect a node failure and be able to clean up after it.

Install

$ npm install --save clusterix

Usage

Run this basic setup in 3 different node processes, for i=1,2,3:

import Clusterix from 'clusterix';
import Redis from 'ioredis'

const ioredis = new Redis({ keyPrefix: 'myapp:clusterix' });
const cluster = new Clusterix(ioredis);
cluster.initializeNode(`node${i}`);
cluster.on('node down', async node => {
  console.log(`${node} is down, ${await cluster.nodes} are still up`);
});

After you kill one on the processes, for instance, node3, either node1 or node2 will output node3 is down, node1,node2 are still up.

API

class Clusterix extends EventEmitter

new Clusterix(redis, { id, nodeId, heartbeatInterval, pollInterval, timeout })

redis

Type: object

Instance of ioredis used to store heartbeats. Should have an open connection to the redis server when calling initializeNode. Lua scripting features of ioredis are used to define a command polls redis for each node's heartbeat, under the name __clusterix__poll. node_redis is not supported. This is because of the lack of built in Promise support on redis commands, and ease of lua commands usage with ioredis.

id

Type: string, optional, default: ''

The identifier for the cluster, can remain empty if using only one cluster (usually that's the case).

nodeId

Type: string, default: ${os.hostname()}:${process.env.PORT}

The id of the node that initialized the instance of clusterix. Has to be unique across the cluster.

heartbeatInterval

Type: integer, default: 500

Determines how often we send a heartbeat to redis. Has to be smaller than the timeout passed to the constructor.

pollInterval

Type: integer, default: 500

How often we poll the server for dead nodes in milliseconds.

timeout

Type: integer, default: 1000

The number of milliseconds before a node is considered down.

initializeNode()

Type: Promise<undefined | Error>

Starts polling for dead nodes and sending heartbeats to redis.

If another node is sending heartbeats under the same nodeId, it gets rejected with an Error.

If heartbeats are found on redis for this same node, but no other node is sending heartbeats, a node down event is emitted.

dispose()

Type: void

Clears all open handles (interval timeouts).

nodes

Type: Promise<Array<string>>

Returns the ids of all the nodes currently in the cluster

Tests

npm test

TODO

  • Extend API
  • More tests
  • Examples

License

See the LICENSE file for license rights and limitations (MIT).