balancer

package
v0.0.0-...-9b87759 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2023 License: BSD-2-Clause Imports: 7 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"
)
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 NewBounded

func NewBounded(hosts []string) Balancer

NewBounded create new Bounded balancer

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 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 {
	sync.RWMutex
	// 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)

Balance selects a suitable host according

func (*BaseBalancer) Done

func (b *BaseBalancer) Done(_ string)

Done .

func (*BaseBalancer) Inc

func (b *BaseBalancer) Inc(_ string)

Inc .

func (*BaseBalancer) Remove

func (b *BaseBalancer) Remove(host string)

Remove new host from the balancer

type Bounded

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

Bounded refers to consistent hash with bounded

func (*Bounded) Add

func (b *Bounded) Add(host string)

Add new host to the balancer

func (*Bounded) Balance

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

Balance selects a suitable host according to the key value

func (*Bounded) Done

func (b *Bounded) Done(host string)

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

func (*Bounded) Inc

func (b *Bounded) Inc(host string)

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

func (*Bounded) Remove

func (b *Bounded) Remove(host string)

Remove new host from the balancer

type Consistent

type Consistent struct {
	BaseBalancer
	// 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) 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 {
	BaseBalancer
}

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

func (*IPHash) Balance

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

Balance selects a suitable host according

type LeastLoad

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

LeastLoad will choose a host based on the least load host

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 {
	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 {
	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