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 NewLeastLoad ¶
NewLeastLoad create new LeastLoad balancer
func NewRoundRobin ¶
NewRoundRobin create new RoundRobin balancer
type BaseBalancer ¶
type BaseBalancer struct {
// contains filtered or unexported fields
}
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 IPHash ¶
type IPHash struct {
BaseBalancer
}
IPHash will choose a host based on the client's IP address IP哈希算法,负载均衡器将请求根据IP地址将其定向分发到后端的目标主机中 对主机数量进行取模,即 CRC32(IP) % len(hosts),则可得到代理的主机
type LeastLoad ¶
type LeastLoad struct {
// contains filtered or unexported fields
}
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), 最后选择其中负载量较小的节点
type Random ¶
type Random struct { BaseBalancer // contains filtered or unexported fields }
Random will randomly select a http server from the server 随机算法,负载均衡器将请求随机分发到后端的目标主机中
type RoundRobin ¶
type RoundRobin struct { BaseBalancer // contains filtered or unexported fields }
RoundRobin will select the server in turn from the server to proxy 轮询算法,负载均衡器将请求依次分发到后端每一台主机中
Click to show internal directories.
Click to hide internal directories.