switches

A distributed system for on/off switches

npm install switches
2 downloads in the last week
8 downloads in the last month

Switches

Build Status

Switches is a distributed, eventually consistent, circuit breaker type system for applications. It enables you to turn features on and off while they're running without having to take them completely offline or do a complete deploy

What follows is a novel example where two Switches instances are synchronized via streams. Seeing how synchronization is done via streams the same synchronization interface can be used to synchronize the instances over TCP, HTTP, Websockets or any other transport with a streaming interface

var Switches = require("switches");
var defaults = require("./defaults");

// create two isolated switches instances with sane defaults
var switches1 = new Switches(defaults);
var switches2 = new Switches(defaults);

// connect the two instances together via streams
var s1 = switches1.createStream();
var s2 = switches2.createStream();
s1.pipe(s2).pipe(s1);

// convenience print status function
var printEnabled = function(switches, name) {
    var status = (switches.isEnabled(name)) ? "enabled" : "disabled";
    console.log(name + ": " + status);
}

// check if registration is enabled in `switches2`
printEnabled(switches2, "registration");

// disable registration in `switches1`
switches1.disable("registration");

process.nextTick(function() {
    // check if registration is enabled in `switches2` and `switches1`
    printEnabled(switches2, "registration");
    printEnabled(switches1, "registration");

    // disable registration via `switches2`
    switches2.enable("registration");

    process.nextTick(function() {
        printEnabled(switches1, "registration");
    });
});

API

Switches

The Switches class is what is directly exported by this library and is responsible for maintaining and synchronizing the state of the switches

Switches::constructor(@defaults = {})

The constructor is responsible for reveiving an object of the sane defaults for all the switches and setting up the initial state of the switches. The @defaults object should be a flat object where all values are either boolean true to represent enabled or boolean false to represent a disabled.

  • @param {Object} defaults - The sane defaults object

Switches::enable(property)

This function is used to enable a parameter, causing remote synchronization and subsequent calls to Switches::isEnabled() with the same property name to return boolean true. If the supplied property parameter is not found in the @defaults map, it is considered to not exist, so no property will become enabled and this function will return boolean false for failure

  • @param {String} property - The property to enable
  • @returns {Boolean} - True on success false on failure

Switches::disable(property)

This function is used to disable a parameter, causing remote synchronization and subsequent calls to Switches::isEnabled() with the same property name to return boolean false. If the supplied property parameter is not found in the @defaults map, it is considered to not exist, so no property will become enabled and this function will return boolean false for failure

Switches::isEnabled(property)

This function returns boolean true if the supplied property parameter is enabled, boolean false otherwise. Also, if the property parameter can not be found in the @defaults map, it is considered to not exist so false is assumed

  • @param {String} property - The property to check
  • @returns {Boolean} - True on enabled false on disabled or missing

Switches::exists(property)

This function is used to determine the existence of a propety. It returns boolean true on existence, boolean false otherwise

  • @param {String} property - The property to check
  • @returns {Boolean} - True on existence false on non-existence

Switches::createStream()

This function returns a duplex stream which can be used to sync this Switches instance with remote Switches instances in an eventually consistent manner

  • @returns {Stream} - The duplex stream to this object

Events 'update', (@self)

This event gets emitted from a Switches instance every time a property value is changed remotely or locally. The only argument passed to event handlers is the Switches instance itself

npm loves you