socket-load-balancer

0.0.9 • Public • Published

socket-load-balancer

NPM Version

This is a load balancer that works at TCP/Unix Socket level and distributes connections across different servers.

Example:

var slb = require("socket-load-balancer");
 
var router = slb.routers.RoundRobin({
    routes: [
        {port: 3001},
        {port: 3002}
    ]
});
 
var server = slb.Server({
    router: router
})
server.listen(3000);

Router

The router is the object responsible of decide to which destination forward a request.

A router can be a function:

var slb = require("socket-load-balancer");
 
var server = slb.Server({
    router: function (request) {
        request.forward({port: 3001}); // not so smart router
    }
})
server.listen(3000);

or can be an object with a manage method

var slb = require("socket-load-balancer");
 
var server = slb.Server({
    router: {
        manage: function (request) {
            request.forward({port: 3001}); // still not so smart router
        }
    }
})
server.listen(3000);

There are already 2 Routers available:

  • RoundRobin Dispatches requests to a group of routes in a Round-Robin fashion (every request is forwarded to a different route in a circular order)
  • LeastUsed Dispatches requests to a group of routes in a Least Used fashion (every request is forwarded to the route that has the lowest number of active connections)

They both have a common interface:

  • addRoute( route ) adds a new route
  • removeRoute( route ) removes an existing route

Timeout

You can optionally activate a timeout in order to close connections if unused for a certain ammount of time.

var slb = require("socket-load-balancer");
 
var router = slb.routers.RoundRobin({
   routes: [
       {port: 3001},
       {port: 3002}
   ]
});
 
var server = slb.Server({
   router: router
})
server.setTimeout(120000);
server.listen(3000);

Readme

Keywords

Package Sidebar

Install

npm i socket-load-balancer

Weekly Downloads

3

Version

0.0.9

License

none

Last publish

Collaborators

  • b3rn475