Version: v0.0.0-...-4deb780 Latest Latest

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

Go to latest
Published: Aug 1, 2022 License: MIT Imports: 5 Imported by: 2



Package balancer provides a simple interface to create concrete balancer algorightms that can be used to choose an upstream



This section is empty.


View Source
var (
	// ErrEmptyBackendList is used when the list of beckends is empty
	ErrEmptyBackendList = errors.New("can not elect backend, Backends empty")
	// ErrCannotElectBackend is used a backend cannot be elected
	ErrCannotElectBackend = errors.New("cant elect backend")
	// ErrUnsupportedAlgorithm is used when an unsupported algorithm is given
	ErrUnsupportedAlgorithm = errors.New("unsupported balancing algorithm")
View Source
var (
	// ErrZeroWeight is used when there a zero value weight was given
	ErrZeroWeight = errors.New("invalid backend, weight 0 given")


This section is empty.


type Balancer

type Balancer interface {
	Elect(hosts []*Target) (*Target, error)

Balancer holds the load balancer methods for many different algorithms

func New

func New(balance string) (Balancer, error)

New creates a new Balancer based on balancing strategy

type RoundrobinBalancer

type RoundrobinBalancer struct {
	// contains filtered or unexported fields

RoundrobinBalancer balancer

func NewRoundrobinBalancer

func NewRoundrobinBalancer() *RoundrobinBalancer

NewRoundrobinBalancer creates a new instance of Roundrobin

func (*RoundrobinBalancer) Elect

func (b *RoundrobinBalancer) Elect(hosts []*Target) (*Target, error)

Elect backend using roundrobin strategy

type Target

type Target struct {
	Target string
	Weight int

Target is an ip address/hostname with a port that identifies an instance of a backend service

type WeightBalancer

type WeightBalancer struct{}

WeightBalancer balancer

func NewWeightBalancer

func NewWeightBalancer() *WeightBalancer

NewWeightBalancer creates a new instance of WeightBalancer

func (*WeightBalancer) Elect

func (b *WeightBalancer) Elect(hosts []*Target) (*Target, error)

Elect backend using weight strategy

Jump to

Keyboard shortcuts

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