balancer

package
v0.1.0-alpha Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2023 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

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

Variables

View Source
var (
	ErrNoHost           = errors.New("no host")
	ErrAlgoNotSupported = 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 NewLeastLoad

func NewLeastLoad(hosts []string) Balancer

NewLeastLoad create new LeastLoad 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 to the key value

func (*BaseBalancer) Done

func (b *BaseBalancer) Done(_ string)

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

func (*BaseBalancer) Inc

func (b *BaseBalancer) Inc(_ string)

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

func (*BaseBalancer) Remove

func (b *BaseBalancer) 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) Done

func (b *Consistent) Done(host string)

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

func (*Consistent) Inc

func (b *Consistent) Inc(host string)

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

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

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 to the key value

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 to the key value

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 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 to the key value

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 to the key value

Jump to

Keyboard shortcuts

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