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

package bounded

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

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/number_of_hosts)*imbalance_constant

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: https://medium.com/vimeo-engineering-blog/improving-load-balancing-with-a-new-consistent-hashing-algorithm-9f1bd75709ed

https://research.googleblog.com/2017/04/consistent-hashing-with-bounded-loads.html

Code:

hosts := []string{"127.0.0.1:8009", "127.0.0.1:8008", "127.0.0.1:8007"}

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 {
        log.Fatal(err)
    }
    // do work for "host"
    fmt.Printf("Send request #%d to host %s\n", i, host)
    // when the work assign to the host is done
    lb.Done(host)
}

Index

Examples

Package Files

bounded.go

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). Updated 2017-07-19. Refresh now. Tools for package owners.