liblb: github.com/lafikl/liblb/r2 Index | Examples | Files

package r2

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

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

Code:

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

Index

Examples

Package Files

r2.go

type R2 Uses

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

func New Uses

func New(hosts ...string) *R2

func (*R2) Add Uses

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 Uses

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 Uses

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

func (*R2) Exists Uses

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

Check if host already exist

func (*R2) Remove Uses

func (rb *R2) Remove(host string)

Package r2 imports 2 packages (graph). Updated 2017-06-17. Refresh now. Tools for package owners.