balancer

package
v0.0.2 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const (
	IPHashBalancer         = "ip-hash"
	ConsistentHashBalancer = "consistent-hash"
	P2CBalancer            = "p2c"
	RandomBalancer         = "random"
	R2Balancer             = "round-robin"
	LeastLoadBalancer      = "least-load"
	BoundedBalancer        = "bounded"
)

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)
}

func Build

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

Build generates the corresponding Balancer according to the algorithm

func NewIPHash

func NewIPHash(hosts []string) Balancer

NewIPHash create new IPHash balancer

func NewLeastLoad

func NewLeastLoad(hosts []string) Balancer

NewLeastLoad create new LeastLoad 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 BaseBalancer

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

func (*BaseBalancer) Add

func (b *BaseBalancer) Add(host string)

Add new host to the balancer

func (*BaseBalancer) Balance

func (b *BaseBalancer) Balance(key string) (string, error)

func (*BaseBalancer) Done

func (b *BaseBalancer) Done(_ string)

func (*BaseBalancer) Inc

func (b *BaseBalancer) Inc(_ string)

func (*BaseBalancer) Remove

func (b *BaseBalancer) Remove(host string)

Remove new host from the balancer

type Factory

type Factory func([]string) Balancer

type IPHash

type IPHash struct {
	BaseBalancer
}

IPHash will choose a host based on the client's IP address IP哈希算法,负载均衡器将请求根据IP地址将其定向分发到后端的目标主机中 对主机数量进行取模,即 CRC32(IP) % len(hosts),则可得到代理的主机

func (*IPHash) Balance

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

Balance selects a suitable host according

type LeastLoad

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

func (*LeastLoad) Add

func (l *LeastLoad) Add(hostName string)

Add new host to the balancer

func (*LeastLoad) Balance

func (l *LeastLoad) Balance(_ string) (string, error)

Balance selects a suitable host according

func (*LeastLoad) Done

func (l *LeastLoad) Done(hostName string)

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

func (*LeastLoad) Inc

func (l *LeastLoad) Inc(hostName string)

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

func (*LeastLoad) Remove

func (l *LeastLoad) Remove(hostName string)

Remove new host from the balancer

type P2C

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

P2C refer to the power of 2 random choice 若请求IP为空,P2C均衡器将随机选择两个代理主机节点,最后选择其中负载量较小的节点 若请求IP不为空,P2C均衡器通过对IP地址以及对IP地址加盐进行CRC32哈希计算 则会得到两个32bit的值,将其对主机数量进行取模,即CRC32(IP) % len(hosts) 、CRC32(IP + salt) % len(hosts), 最后选择其中负载量较小的节点

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(key string)

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

func (*P2C) Inc

func (p *P2C) Inc(key string)

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

func (*P2C) Remove

func (p *P2C) Remove(hostName string)

Remove new host from the balancer

type Random

type Random struct {
	BaseBalancer
	// contains filtered or unexported fields
}

Random will randomly select a http server from the server 随机算法,负载均衡器将请求随机分发到后端的目标主机中

func (*Random) Balance

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

Balance selects a suitable host according

type RoundRobin

type RoundRobin struct {
	BaseBalancer
	// contains filtered or unexported fields
}

RoundRobin will select the server in turn from the server to proxy 轮询算法,负载均衡器将请求依次分发到后端每一台主机中

func (*RoundRobin) Balance

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

Balance selects a suitable host according

Jump to

Keyboard shortcuts

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