liblb: Index | Examples | Files

package r2

import ""

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


lb := r2.New("", "", "")
for i := 0; i < 10; i++ {
    host, err := lb.Balance()
    if err != nil {
    fmt.Printf("Send request #%d to host %s\n", i, host)



Package Files


type R2 Uses

type R2 struct {
    // 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) and is imported by 1 packages. Updated 2017-06-17. Refresh now. Tools for package owners.