r2

package
v0.0.0-...-2321f9d Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2017 License: MIT Imports: 2 Imported by: 2

README

Round Robin Balancer

It's one of the simplest if not the simplest load balancing algorithm. It distribute requests by walking the list of servers and assigning a request to each server in turn. On the downside Round-Robin assumes that all servers are alike, and that all requests take the same amount of time, which is obviously not true in practice.

Usage Example:
package main

import (
	"fmt"
	"log"
	"testing"

	"github.com/lafikl/liblb/r2"
)

func main() {
	lb := r2.New("127.0.0.1:8009", "127.0.0.1:8008", "127.0.0.1:8007")
	for i := 0; i < 10; i++ {
		host, err := lb.Balance()
		if err != nil {
			log.Fatal(err)
		}
		fmt.Printf("Send request #%d to host %s\n", i, host)
	}
}

Weighted Round Robin

A variant of Round-Robin that assigns a weight for every host, the weight affects the number of requests that gets sent to the server. Assume that we have two hosts A with weight 1 and B with weight 4, that means for every single request we send to A we send 4 requests to B. In other words, 80% of the requests would go to B. Which you can calculate by yourself applying this formula host_weight/total_weights.

Usage Example:
package main

import (
	"fmt"
	"log"
	"testing"

	"github.com/lafikl/liblb/r2"
)

func main() {
    // default weight is 1
	lb := r2.New("127.0.0.1:8009", "127.0.0.1:8008", "127.0.0.1:8007")
    // host_weight/total_weights
    // this hosts load would be 3/(3+3)=0.5
    // meaning that 50% of the requests would go to 127.0.0.1:9000
    lb.AddWeight("127.0.0.1:9000", 3)
	for i := 0; i < 10; i++ {
		host, err := lb.Balance()
		if err != nil {
			log.Fatal(err)
		}
		fmt.Printf("Send request #%d to host %s\n", i, host)
	}
}

Documentation

Overview

R2 is a concurrency-safe Round-Robin Balancer. Which also supports Weighted Round-Robin.

Round-Robin is a simple and well known algorithm for load balancing.

Here's a simple implmentation of it, if you're not already familiar with it

https://play.golang.org/p/XCMAtKGCaE

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type R2

type R2 struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func New

func New(hosts ...string) *R2

func (*R2) Add

func (rb *R2) Add(host string)

Adds a host to the list of hosts, with the weight of the host being 1.

func (*R2) AddWeight

func (rb *R2) AddWeight(host string, weight int)

Weight increases the percentage of requests that get sent to the host Which can be calculated as `weight/(total_weights+weight)`.

func (*R2) Balance

func (rb *R2) Balance() (string, error)

func (*R2) Exists

func (rb *R2) Exists(host string) bool

Check if host already exist

func (*R2) Remove

func (rb *R2) Remove(host string)

Jump to

Keyboard shortcuts

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