liblb: Index | Examples | Files

package p2c

import ""

P2C will distribute the traffic by choosing two hosts either via hashing or randomly and then pick the least loaded of the two. It gaurantees that the max load of a server is ln(ln(n)), where n is the number of servers.

All operations in P2C are concurrency-safe.

For more info:


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

// Power of Two choices example
lb := p2c.New(hosts...)
for i := 0; i < 10; i++ {
    // uses random power of two choices, because the key length == 0
    host, err := lb.Balance("")
    if err != nil {
    // load should be around 33% per host
    fmt.Printf("Send request #%d to host %s\n", i, host)
    // when the work assign to the host is done

// Partial Key Grouping example
pp2c := p2c.New(hosts...)
for i := 0; i < 10; i++ {
    // uses PKG because the key length is > 0
    host, err := pp2c.Balance("hello world")
    if err != nil {

    // traffic should be split between two nodes only
    fmt.Printf("Send request #%d to host %s\n", i, host)
    // when the work assign to the host is done




Package Files


type P2C Uses

type P2C struct {
    // contains filtered or unexported fields

func New Uses

func New(hosts ...string) *P2C

New returns a new instance of RandomTwoBalancer

func (*P2C) Add Uses

func (p *P2C) Add(hostName string)

func (*P2C) Balance Uses

func (p *P2C) Balance(key string) (string, error)

Balance picks two servers either randomly (if no key supplied), or via hashing (PKG) if given a key, then it returns the least loaded one between the two.

Partial Key Grouping (PKG) is great for skewed data workloads, which also needs to be determinstic in the way of choosing which servers to send requests too. the maximum load of a server in PKG at anytime is: `max_load-avg_load`

func (*P2C) Done Uses

func (p *P2C) Done(host string)

Decrments the load of the host (if found) by 1

func (*P2C) GetLoad Uses

func (p *P2C) GetLoad(host string) (load uint64, err error)

Returns the current load of the server, or it returns liblb.ErrNoHost if the host doesn't exist.

func (*P2C) Remove Uses

func (p *P2C) Remove(host string)

func (*P2C) UpdateLoad Uses

func (p *P2C) UpdateLoad(host string, load uint64) error

UpdateLoad updates the load of a host

Package p2c imports 6 packages (graph) and is imported by 1 packages. Updated 2017-07-03. Refresh now. Tools for package owners.