awesome-balancer

1.0.16 • Public • Published

awesome-balancer

awesome-balancer is a load-balance engine included various strategy. It is a collection of load balancing algorithm implementation, which is focus on providing reusable module for other program.

awesome-balancer 是一个包含了多种负载均衡策略的核心引擎,它实现了很多负载均衡的算法,致力于为其他软件程序提供可复用的负载均衡模块。

该负载均衡引擎可支持类似于反向代理作业调度的场景。其中的DynamicWeightedEngine动态权重引擎依赖于resource-meter实现了基于集群实时资源状况的动态负载均衡。

Get Started

With node and npm in your system, and in your project to execute:

npm install awesome-balancer --save

add to your program

var lb = require('awesome-balancer')

Usage

To use, instantiate an engine or call a factory method with a pool. Then call pick(), which will return the selected object, calling pick() repeatedly will yield the same or a different object from the pool, depending on the algorithm which powers that engine.

var engine = lb.random(['a', 'b', 'c'])
var pick = engine.pick()

pick()

Pick is called without any arguments and will always return an object which is a member of the pool.

Engines

Random Engine

The random engine picks an object from the pool at random, each time pick() is called.

var lb = require('loadbalance')
var engine = lb.random(['a', 'b', 'c'])
var pick = engine.pick()

You can also use as a class: new RandomEngine(pool, seed)

var engine = new lb.RandomEngine(pool)

Pool - an objects to pick from, eg [1,2,3] Seed - an optional seed that will be used to recreate a random sequence of selections

RoundRobinEngine

An engine that picks objects from its pool using Round Robin algorithm (doh!)

var engine = lb.roundRobin(['a', 'b', 'c'])
var pick = engine.pick()

The roundRobin() factory method can be used to obtain both RoundRobinEngine and WeightedRoundRobinEngine. The decision is based on the contents of the pool.

You can also use as a class: new RoundRobinEngine(pool)

var engine = new lb.RoundRobinEngine(pool)

Pool - objects to pick from, eg [1,2,3]

WeightedRoundRobinEngine

Same as round robin engine, only members of the pool can have weights.

var engine = lb.roundRobin([{ object: 'a', weight: 2 }, {object: 'b', weight: 1 }])
var pick = engine.pick()

call pick six times using the above engine will yield: 'a', 'a', 'b', 'a', 'a', 'b'

You can also use as a class: new WeightedRoundRobinEngine(pool)

var engine = new lb.WeightedRoundRobinEngine(pool)

Pool - objects to pick from. Each object is of the form:

var object1 = {
    object: 'something',
    weight: 2
}
var object1 = {
    value: 'something',
    weight: 2
}

Weight should always be an integer which is greater than zero. Object (you can also use value, its an alias property) can be anything you want, just like other pools. It cannot, however, be null or undefined at the time the pool is created.

DynamicWeightedEngine

Familiar with the WeightedRoundRobinEngine, but its weight of pool will be change dynamic each cycle.

This algorithm use resource-meter project to make the resource weight dynamic. So, first you should start the resource-meter probe on your clusters' nodes.(See probe mode to konw how start the resource-meter probe ) And then, on the master node, you can use DynamicWeightedEngine like this:

var pool = [{value: 'xxx', weight: 1}, {value: 'xxx', weight: 1}];
var engine = lb.dynamicWeighted(pool)
var pick = engine.pick()

You can also use as a class: new RoundRobinEngine(pool)

var engine = new lb.BusinessDivision(pool)

As you see above, you should pass the nodes with a initial property 'weight' (for example weight:1), but , some time later, the weight will be change dynamically by the nodes' runtime resource. [get info by resource-meter probe]. Even so, you still should pass the weight property at first time.

BusinessDivisionEngine

An engine that divide the pool members by its type, and in each type picking the object using a specify engine.

var engine = lb.businessDivision([{type: 'one', value: 'a'}, {type: 'two', value: 'b'}], 'RandomEngine')
var pick = engine.pick()

You can also use as a class: new RoundRobinEngine(pool)

var engine = new lb.BusinessDivision(pool)

The BusinessDivisionEngine can pass the second parameter--'engineName'. For example, if you want to use 'RandomEngine' in each business type, you should create the engine like this:

var engine = new lb.BusinessDivision(pool, 'RandomEngine')

Attention,you can not pass the engine "BusinessDivisionEngine",because we do not support recursion.

When you use pick(), you should pass the 'type' paramerters like this:

engine.pick('type')

The 'type' should as same as the 'type' you set the pool.

PriorityEngine

Not yet implemented

Extensibility

Here is an example of a custom engine:

var AbstractEngine = require('awesome-balancer').AbstractEngine
var inherits = require('util').inherits
 
function MyEngine(pool) {
    AbstractEngine.call(this, pool)
}
 
inherits(MyEngine, AbstractEngine)
 
MyEngine.prototype.pick = function () {
    // pick something from the pool somehow and return it
}
 

The contract of pick() states that it MUST return something each invocation.

test

npm test

Please specify the DEBUG environment variable to be "awesomeBalancer:*", to display the debug messages.

misc

This module is created on the basis of node-balance. awesome-balancer extends from it and enhance it.

This module is heavily inspired by this article about load balance algorithms

license

This software is free to use under the MIT license. See the LICENSE file for license text and copyright information.

Copyright © 2016 cuiyongjian [email protected]

Package Sidebar

Install

npm i awesome-balancer

Weekly Downloads

2

Version

1.0.16

License

MIT

Last publish

Collaborators

  • cuiyongjian