balancer

package
v0.0.0-...-0b5f1db Latest Latest
Warning

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

Go to latest
Published: May 25, 2022 License: BSD-2-Clause Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	IPHashBalancer         = "ip-hash"
	ConsistentHashBalancer = "consistent-hash"
	P2CBalancer            = "p2c"
	RandomBalancer         = "random"
	R2Balancer             = "round-robin"
)
View Source
const Salt = "%#!"

Variables

View Source
var (
	NoHostError                = errors.New("no host")
	AlgorithmNotSupportedError = errors.New("algorithm not supported")
)

Functions

This section is empty.

Types

type Balancer

type Balancer interface {
	Add(string)
	Remove(string)
	Balance(string) (string, error)
	Inc(string)
	Done(string)
}

Balancer interface is the load balancer for the reverse proxy

func Build

func Build(algorithm string, hosts []string) (Balancer, error)

Build generates the corresponding Balancer according to the algorithm

func NewConsistent

func NewConsistent(hosts []string) Balancer

NewConsistent create new Consistent balancer

func NewIPHash

func NewIPHash(hosts []string) Balancer

NewIPHash create new IPHash balancer

func NewP2C

func NewP2C(hosts []string) Balancer

NewP2C create new P2C balancer

func NewRandom

func NewRandom(hosts []string) Balancer

NewRandom create new Random balancer

func NewRoundRobin

func NewRoundRobin(hosts []string) Balancer

NewRoundRobin create new RoundRobin balancer

type Consistent

type Consistent struct {
	// contains filtered or unexported fields
}

Consistent refers to consistent hash

func (*Consistent) Add

func (c *Consistent) Add(host string)

Add new host to the balancer

func (*Consistent) Balance

func (c *Consistent) Balance(key string) (string, error)

Balance selects a suitable host according to the key value

func (*Consistent) Done

func (c *Consistent) Done(_ string)

Done .

func (*Consistent) Inc

func (c *Consistent) Inc(_ string)

Inc .

func (*Consistent) Remove

func (c *Consistent) Remove(host string)

Remove new host from the balancer

type Factory

type Factory func([]string) Balancer

Factory is the factory that generates Balancer, and the factory design pattern is used here

type IPHash

type IPHash struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

IPHash will choose a host based on the client's IP address

func (*IPHash) Add

func (r *IPHash) Add(host string)

Add new host to the balancer

func (*IPHash) Balance

func (r *IPHash) Balance(key string) (string, error)

Balance selects a suitable host according

func (*IPHash) Done

func (r *IPHash) Done(_ string)

Done .

func (*IPHash) Inc

func (r *IPHash) Inc(_ string)

Inc .

func (*IPHash) Remove

func (r *IPHash) Remove(host string)

Remove new host from the balancer

type P2C

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

P2C refer to the power of 2 random choice

func (*P2C) Add

func (p *P2C) Add(hostName string)

Add new host to the balancer

func (*P2C) Balance

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

Balance selects a suitable host according to the key value

func (*P2C) Done

func (p *P2C) Done(host string)

Done refers to the number of connections to the server `-1`

func (*P2C) Inc

func (p *P2C) Inc(host string)

Inc refers to the number of connections to the server `+1`

func (*P2C) Remove

func (p *P2C) Remove(host string)

Remove new host from the balancer

type Random

type Random struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Random will randomly select a http server from the server

func (*Random) Add

func (r *Random) Add(host string)

Add new host to the balancer

func (*Random) Balance

func (r *Random) Balance(_ string) (string, error)

Balance selects a suitable host according

func (*Random) Done

func (r *Random) Done(_ string)

Done .

func (*Random) Inc

func (r *Random) Inc(_ string)

Inc .

func (*Random) Remove

func (r *Random) Remove(host string)

Remove new host from the balancer

type RoundRobin

type RoundRobin struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

RoundRobin will select the server in turn from the server to proxy

func (*RoundRobin) Add

func (r *RoundRobin) Add(host string)

Add new host to the balancer

func (*RoundRobin) Balance

func (r *RoundRobin) Balance(_ string) (string, error)

Balance selects a suitable host according

func (*RoundRobin) Done

func (r *RoundRobin) Done(_ string)

Done .

func (*RoundRobin) Inc

func (r *RoundRobin) Inc(_ string)

Inc .

func (*RoundRobin) Remove

func (r *RoundRobin) Remove(host string)

Remove new host from the balancer

Jump to

Keyboard shortcuts

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