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

followtheleader

v2.0.9

Published

Zero-conf leader election based on bonjour

Downloads

39

Readme

Follow the Leader!

The zero-conf leader election library using Bonjour for service discovery.

CircleCI

Installation

npm install followtheleader -S

Usage

import { elect, Elector } from 'followtheleader';

console.log('Starting leader election');
const elector: Elector = elect("MyServiceName");
elector.on('leader', () => {
  console.log('This node was elected as leader');
});
elector.on('follower', () => {
  console.log('this node was elected as follower');
});
elector.on('reelection', () => {
  console.log('master node is unavailable, restarting election process');
});
elector.on('error', (error) => {
  console.log(error);
});
elector.start();

API

Initializing

// ES6
import { elect, Elector } from 'followtheleader';
const elector = elect(options));

// CommonJS
const ftl = require('followtheleader');
const elector = ftl.elect(options);

options are:

  • name (string, required)
  • type (string, optional) - defaults to 'udp'
  • port (number, optional) - defaults to 9001
  • host (string, optional) - defaults to '0.0.0.0'
  • maxFailedResponses (number, optional) - defaults to 3

The option values can either by provided as arguments or as an object.

Starting election process

elector.start()

Starts the election process. If the node is elected leader it will start a heartbeat monitor that binds an UDP listener to the specified port and host. If the node is elected follower, it will ping the master node. If maxFailedResponses is reached, the node will restart the election process.

Event: leader

The node has been elected leader.

Event: follower

The node has been elected follower.

Event: reelection

The master node has become unavailable and the election process is restarted.

Event: error

An error occurred somewhere during the election proces or in the heartbeat monitor.

Caveats

Zero-conf / Bonjour / mDNS service discovery will only work for nodes that are on the same network subnet mask. If your nodes run on servers spreading multiple geographical locations, on different networks or if you are using a diffferent VLAN per server, you should look for other solutions. A good alternative would be Elector which uses ZooKeeper.

If you are running your application in Amazon Web Services ECS clusters, you might want to try configuring your tasks with the awsvpc network mode. This will give each task their own elastic IP within the selected VPC subnet. This will only work if all ECS nodes are running in the same Availability Zone and subnet. If your cluster is running nodes in multiple Availability Zones, Zero-config/Bonjour/mDNS will not work.

If you're running Kubernetes, you should look into the Kubernetes/Contrib election solution.

License

MIT