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

loadbalance

v1.0.0

Published

loadbalance

Downloads

2,656

Readme

loadbalance

npm status Travis build status Dependency status

This is a collection of load balancing engines in (what is hopefully) their most distilled form.

The goal was to create a highly reusable implementation that imposes as little as possible on the user.

Install

With npm do:

npm i loadbalance

const loadbalance = require('loadbalance')

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.

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

pick()

pick() is called without any arguments and will always return an object which is a member of the pool according to the pool's selection strategy

Engines

Random Engine

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

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

new RandomEngine(pool, seed)

const engine = new loadbalance.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

Weighted Random Engine

The random engine picks an object from the pool at random but with bias (probably should have called it BiasedRandomEngine), each time pick() is called.

const loadbalance = require('loadbalance')
const engine = loadbalance.random([
    { object: 'a', weight: 2 }, 
    { object: 'b', weight: 3 }, 
    { object: 'c', weight: 5 }
])
const pick = engine.pick()

With this engine, calling pick() repeatedly will roughly return 'a' 20% of the time, 'b' 30% of the time and 'c' 50% of the time

new WeightedRandomEngine(pool, seed)

const engine = new loadbalance.WeightedRandomEngine(pool)

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

const object1 = {
    object: 'something',
    weight: 2
}

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!)

const loadbalance = require('loadbalance')
const engine = loadbalance.roundRobin(['a', 'b', 'c'])
const 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.

new RoundRobinEngine(pool)

const engine = new loadbalance.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.

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

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

new WeightedRoundRobinEngine(pool)

const engine = new loadbalance.WeightedRoundRobinEngine(pool)

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

const object1 = {
    object: '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.

PriorityEngine

Not yet implemented

Extensibility

Here is an example of a custom engine:

const AbstractEngine = require('loadbalance').AbstractEngine

class MyEngine{
    constructor(pool) {
        super(pool)
    }

    pick() {
        // pick something from the pool somehow and return it
    }
}

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

misc

This module shares some functinality with pool module. It is worth taking a look at it if you are looking for something more high level.

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

license

MIT © yaniv kessler