slb

package module
v0.0.0-...-c4eb010 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 28, 2018 License: MIT Imports: 12 Imported by: 0

README

Simple Load Balancer CircleCI Go Report Card

slb is a simple single node HTTP load balancer

Available strategies are:

These strategies for the node pools are also concurrent safe.

Demo

Round Robin
bin/slb -strategy round_robin -config config.json

Least Busy
bin/slb -strategy least_busy -config config.json

The pool was tested by a requester that produces tasks at a random interval, faster than the time it takes to complete the requests itself. The tasks also take an inconsistent amount of time to finish. In this example the requests were just sleeps.

The standard deviation measures how spread out the pending requests are across workers, giving us an idea of how well the pool is distributing work. As time goes by the average load goes up the standard deviation remains about the same!

Basically the smaller the Std Dev the better work is being distributed!

It's worth noting that because the requests aren't equal in the amount of work, the round robin balancer's standard deviation starts to climb. While the least busy strategy keeps the amount of pending tasks in account and results in a better distribution of requests.

Try The Demo

# clone the project
go get github.com/JackyChiu/slb
cd $GOPATH/src/github.com/JackyChiu/slb

# make project
make all

# start local servers (only for demo)
bin/servers -config config.json

# start balancer server
bin/slb -strategy <round_robin or least_busy> -config config.json

# start the requester (only for demo)
bin/requester

A json file is used to specify hosts that are in the node pool, and the port that the load balancer to run on.

{
  "port": 8000,
  "hosts": [
    "cool-app01.mydomain.com",
    "cool-app02.mydomain.com",
    "cool-app03.mydomain.com",
    "cool-app04.mydomain.com"
  ]
}

Documentation

Index

Constants

View Source
const (
	LeastBusy  = "least_busy"
	RoundRobin = "round_robin"
)

Available strategies for the pool.

Variables

This section is empty.

Functions

This section is empty.

Types

type Balancer

type Balancer struct {
	*httputil.ReverseProxy
	// contains filtered or unexported fields
}

Balancer is the reverse proxy server that balances requests.

func NewBalancer

func NewBalancer(strategy string, hosts []string) *Balancer

NewBalancer creates a new balancer to balance requests between hosts and uses specified strategy.

func (*Balancer) Director

func (b *Balancer) Director(r *http.Request)

Director directs the request to the node that was dispatched by pool.

func (*Balancer) ModifyResponse

func (b *Balancer) ModifyResponse(res *http.Response) error

ModifyResponse tells the pool that the request was handled.

type Config

type Config struct {
	Port  int      `json:"port"`
	Hosts []string `json:"hosts"`
}

Config models the configuations that is available for the balancer.

func MustParseConfig

func MustParseConfig(configPath string) Config

MustParseConfig parse and config file and panics on failure.

func ParseConfig

func ParseConfig(configPath string) (Config, error)

ParseConfig reads a json file and maps it to a Config object.

type Pool

type Pool interface {
	Dispatch() node
	Complete(res *http.Response)
}

Pool is an interface for pools with different strategies of distributing work.

func NewPool

func NewPool(strategy string, hosts []string) Pool

NewPool provides a pool with specified strategy.

Directories

Path Synopsis
cmd
slb

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL