liblb: Index | Examples | Files

package bounded

import ""

Bounded is Consistent hashing with bounded loads. It acheives that by adding a capacity counter on every host, and when a host gets picked it, checks its capacity to see if it's below the Average Load per Host.

All opertaions in bounded are concurrency-safe.

Average Load Per Host is defined as follows:


totalLoad = sum of all hosts load

load = the number of active requests

imbalance_constant = is the imbalance constant, which is 1.25 in our case

it bounds the load imabalnce to be at most 25% more than (totalLoad/number_of_hosts).

For more info:


hosts := []string{"", "", ""}

lb := bounded.New(hosts...)
// Host load will never exceed (number_of_requests/len(hosts)) by more than 25%
// in this case:
// any host load would be at most:
// ceil((10/3) * 1.25)
for i := 0; i < 10; i++ {
    host, err := lb.Balance("hello world")
    if err != nil {
    // do work for "host"
    fmt.Printf("Send request #%d to host %s\n", i, host)
    // when the work assign to the host is done



Package Files


type Bounded Uses

type Bounded struct {
    // contains filtered or unexported fields

func New Uses

func New(hosts ...string) *Bounded

func (*Bounded) Add Uses

func (b *Bounded) Add(host string)

func (*Bounded) Balance Uses

func (b *Bounded) Balance(key string) (host string, err error)

err can be liblb.ErrNoHost if there's no added hosts.

func (*Bounded) Done Uses

func (b *Bounded) Done(host string)

should be called when an assigned request to host is finished.

func (*Bounded) Inc Uses

func (b *Bounded) Inc(host string)

It should be called once a request is assigned to a host, obtained from b.Balance.

func (*Bounded) Loads Uses

func (b *Bounded) Loads() map[string]int64

func (*Bounded) MaxLoad Uses

func (b *Bounded) MaxLoad() int64

Max load of a host is (Average Load Per Host*1.25)

func (*Bounded) Remove Uses

func (b *Bounded) Remove(host string)

Package bounded imports 2 packages (graph) and is imported by 1 packages. Updated 2017-07-19. Refresh now. Tools for package owners.