p2c

package
v0.0.0-...-35cb34d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 11, 2020 License: MIT Imports: 6 Imported by: 0

README

P2C Balancer

TODO

Usage Example:
package main

import (
	"fmt"
	"log"
	"testing"

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

func main() {
	lb := p2c.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)
	}
}

P2C Balancer

TODO

Usage Example:
package main

import (
	"fmt"
	"log"
	"testing"

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

func main() {
	lb := p2c.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)
	}
}

Documentation

Overview

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: https://brooker.co.za/blog/2012/01/17/two-random.html

http://www.eecs.harvard.edu/~michaelm/postscripts/handbook2001.pdf

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type P2C

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

func New

func New(hosts ...string) *P2C

New returns a new instance of RandomTwoBalancer

func (*P2C) Add

func (p *P2C) Add(hostName string)

func (*P2C) Balance

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. https://arxiv.org/pdf/1510.07623.pdf the maximum load of a server in PKG at anytime is: `max_load-avg_load`

func (*P2C) Done

func (p *P2C) Done(host string)

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

func (*P2C) GetLoad

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

func (p *P2C) Remove(host string)

func (*P2C) UpdateLoad

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

UpdateLoad updates the load of a host

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL