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

awesome-balancer

v1.0.16

Published

a load-balance engine include various strategy

Downloads

8

Readme

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. [LICENSE file]: https://github.com/cuiyongjian/awesome-balancer/blob/master/LICENSE

Copyright © 2016 cuiyongjian [email protected]