/sticky-load-balancer

NPM-Module for Nodejs to create a loadbalancer with a sticky-strategie.

Primary LanguageJavaScriptMIT LicenseMIT

NOTICE

This project is unmaintained. It is currently working as it should and I used it in production with over 250.000 requests/day on a single server. The problem is that the code is ugly, the usage of farmhash is unnecessary. There are no tests. There is no support for websockets. So if you want to help refactoring, make contact. If you want to use the current version, feel save, semver is and will be satisfied.

sticky-load-balancer

Module for nodejs v4+ to create a loadbalancer with a sticky-strategie.

custom sticky strategies

Create own sticky strategies to distribute requests depending on the incoming data. ( cookies, headers, GET, body.. )

Dependencies

This module needs the npm-module 'farmhash'. To use this you need a python compiler. For Linux you can run the following commands to install it. With windows you are screwed.

apt-get install build-essential
apt-get install python
apt-get install g++
npm install node-gyp -g

Installation

npm install sticky-load-balancer --save

Visit

npm | github

Example Code

StickyLoadBalancer = require('sticky-load-balancer');

//set loggin to active
StickyLoadBalancer.setLogging(true);

//create new balancer
var balancer = new StickyLoadBalancer('127.0.0.1', 5555);

/**
 * this must be unique if you chain multiple balancers together.
 * @default Math.random()^5
 */
balancer.setIdentifier('foooooobar');

/**
 * define which parts of a request should be take to create your sticky strategie.
 */
balancer.setStickyStrategy([
    'url',
    'body.foo',
    'body.foobar',
    'headers.user-agent',
    'cookie.foo'
]);

//start the balancer
balancer.start();

Example Code to remotely add one node to the balancer

StickyLoadBalancer = require('sticky-load-balancer');
/**
 * tell the balancer that this node exists
 */
StickyLoadBalancer.tellBalancer(
    {
        //stats of the balancer
        ip: '127.0.0.1',
        port: 5555,
        identifier: 'foooooobar'
    },
    {
        //stats of the node
        ip: '127.0.0.1',
        port: 80,
        balance: 6
    }
);