balancer

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: May 7, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const Salt = "%#!"

Variables

View Source
var (
	NodeHostErr                = errors.New("no host")
	AlgorithmNotSupportedError = errors.New("algorithm not supported")
)
View Source
var (
	P2CBalancer            = "p2c"             // p2c 的全称是 power of two choices 负载均衡算法,原理是从两个节点中选择负载较小的节点作为服务端
	IPHashBalancer         = "ip_hash"         // ip_hash 的全称是 ip hash 负载均衡算法,原理是根据客户端的 IP 地址进行哈希计算,将请求分配给固定的一台服务器
	ConsistentHashBalancer = "consistent_hash" // consistent_hash 的全称是 consistent hash 负载均衡算法,原理是根据请求的 key 进行哈希计算,将请求分配给哈希环上最近的一台服务器
	RandomBalancer         = "random"          // random 的全称是 random 负载均衡算法,原理是随机选择一台服务器作为服务端
	RoundRobinBalancer     = "round_robin"     // round_robin 的全称是 round robin 负载均衡算法,原理是按照节点列表的顺序依次选择一台服务器作为服务端
	LeastLoadBalancer      = "least_load"      // least_load 的全称是 least load 负载均衡算法,原理是选择负载最小的节点作为服务端
	BoundedBalancer        = "bounded"         // bounded 的全称是 bounded 负载均衡算法,原理是限制服务器的最大并发连接数,防止服务器过载
)

Functions

This section is empty.

Types

type Balancer

type Balancer interface {
	// Add 添加一个节点
	Add(string)
	// Remove 移除一个节点
	Remove(string)
	// Balance 负载均衡
	Balance(string) (string, error)
	// Inc 增加一个节点的权重
	Inc(string)
	// Done 一个节点完成了一次请求
	Done(string)
}

Balancer 是一个负载均衡器的接口

func Build

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

Build 根据算法和节点列表创建负载均衡器

func NewBounded

func NewBounded(nodes []string) Balancer

NewBounded 用于创建一个新的 Bounded 负载均衡器

func NewConsistentHash

func NewConsistentHash(nodes []string) Balancer

NewConsistentHash 创建一个一致性哈希的负载均衡器

func NewIPHash

func NewIPHash(nodes []string) Balancer

NewIPHash create new IPHash balancer

func NewLastLoad

func NewLastLoad(hosts []string) Balancer

NewLastLoad 创建一个最小负载均衡器

func NewP2C

func NewP2C(nodes []string) Balancer

NewP2C 创建一个 P2C 负载均衡器

func NewRandom

func NewRandom(nodes []string) Balancer

NewRandom 创建一个随机负载均衡器

func NewRoundRobin

func NewRoundRobin(nodes []string) Balancer

NewRoundRobin 创建一个轮询负载均衡器

type BaseBalancer

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

BaseBalancer 是一个负载均衡器的基础实现

func (*BaseBalancer) AddNode

func (b *BaseBalancer) AddNode(node string)

AddNode 向负载均衡器中添加一个节点

func (*BaseBalancer) Balance

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

Balance 负载均衡算法的实现

func (*BaseBalancer) Done

func (b *BaseBalancer) Done(_ string)

Done 一个节点完成了一次请求

func (*BaseBalancer) Inc

func (b *BaseBalancer) Inc(_ string)

Inc 增加一个节点的权重

type Bounded

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

func (*Bounded) Add

func (b *Bounded) Add(node string)

Add 用于向 Bounded 负载均衡器中添加一个节点

func (*Bounded) Balance

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

Balance 用于根据 key 选择一个节点

func (*Bounded) Done

func (b *Bounded) Done(node string)

Done 用于完成一次请求

func (*Bounded) Inc

func (b *Bounded) Inc(node string)

Inc 用于增加一个节点的权重

func (*Bounded) Remove

func (b *Bounded) Remove(node string)

Remove 用于从 Bounded 负载均衡器中移除一个节点

type Consistent

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

func (*Consistent) Add

func (c *Consistent) Add(node string)

Add 用于向一致性哈希负载均衡器中添加一个节点

func (*Consistent) Balance

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

Balance 用于从一致性哈希负载均衡器中选择一个节点

func (*Consistent) Remove

func (c *Consistent) Remove(node string)

Remove 用于从一致性哈希负载均衡器中移除一个节点

type Factory

type Factory func([]string) Balancer

Factory 创建一个负载均衡器的工厂函数

type IPHash

type IPHash struct {
	BaseBalancer
}

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

func (*IPHash) Add

func (r *IPHash) Add(node string)

Add 添加节点

func (*IPHash) Balance

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

Balance 是 IPHash 负载均衡算法的实现

func (*IPHash) Remove

func (r *IPHash) Remove(node string)

Remove 删除节点

type LeastLoad

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

func (*LeastLoad) Add

func (l *LeastLoad) Add(node string)

Add 添加节点

func (*LeastLoad) Balance

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

Balance 是最小负载均衡算法的实现

func (*LeastLoad) Done

func (l *LeastLoad) Done(hostName string)

Done 减少节点负载

func (*LeastLoad) Inc

func (l *LeastLoad) Inc(hostName string)

Inc 增加节点负载

func (*LeastLoad) Remove

func (l *LeastLoad) Remove(node string)

Remove 删除节点

type P2C

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

P2C 是一个实现了 P2C 负载均衡算法的结构体

func (*P2C) Add

func (p *P2C) Add(node string)

Add 向 P2C 负载均衡器中添加一个节点

func (*P2C) Balance

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

Balance 是 P2C 负载均衡算法的实现

func (*P2C) Done

func (p *P2C) Done(node string)

Done 用于减少节点的负载

func (*P2C) Inc

func (p *P2C) Inc(node string)

Inc 用于增加节点的负载

func (*P2C) Remove

func (p *P2C) Remove(node string)

Remove 从 P2C 负载均衡器中移除一个节点

type Random

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

func (*Random) Add

func (r *Random) Add(node string)

Add 添加节点

func (*Random) Balance

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

Balance 实现负载均衡接口

func (*Random) Remove

func (r *Random) Remove(node string)

Remove 移除节点

type RoundRobin

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

func (*RoundRobin) Add

func (r *RoundRobin) Add(node string)

Add 添加节点

func (*RoundRobin) Balance

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

Balance 轮询负载均衡器

func (*RoundRobin) Remove

func (r *RoundRobin) Remove(node string)

Remove 移除节点

Jump to

Keyboard shortcuts

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