Version: v0.13.207 Latest Latest

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

Go to latest
Published: May 17, 2022 License: Apache-2.0, MIT Imports: 20 Imported by: 10



Package loadbalancer implements load balancer algorithms that are applied by the proxy.

roundRobin Algorithm

The roundRobin algorithm does proxy requests round robin to
backend endpoints. It has a mutex to update the index and will
start at a random index

random Algorithm

The random algorithm does proxy requests to random backend

consistentHash Algorithm

The consistentHash algorithm choose backend endpoints by hashing
client data with hash function fnv.New32. The client data is the
client IP, which will be looked up from X-Forwarded-For header
with remote IP as the fallback.

powerOfRandomNChoices Algorithm

The powerOfRandomNChoices algorithm selects N random endpoints
and picks the one with least outstanding requests from them.
Currently, N is 2.

The roundRobin and the random algorithms also provide fade-in behavior for LB endpoints of routes where the fade-in duration was configured. This feature can be used to gradually add traffic to new instances of applications that require a certain amount of warm-up time.

Eskip example:

r1: * -> <roundRobin, "", "">;
r2: * -> <consistentHash, "", "">;
r3: * -> <random, "", "">;
r4: * -> <powerOfRandomNChoices, "", "">;

Package loadbalancer also implements health checking of pool members for a group of routes, if backend calls are reported to the loadbalancer.

Based on https://landing.google.com/sre/book/chapters/load-balancing-datacenter.html#identifying-bad-tasks-flow-control-and-lame-ducks-bEs0uy we use

Healthy (healthy)

The backend task has initialized correctly and is processing

Refusing connections (dead)

The backend task is unresponsive. This can happen because the
task is starting up or shutting down, or because the backend is
in an abnormal state (though it would be rare for a backend to
stop listening on its port if it is not shutting down).

Lame duck (unhealthy)

The backend task is listening on its port and can serve, but is
explicitly asking clients to stop sending requests.



View Source
const (
	ConsistentHashKey           = "consistentHashKey"
	ConsistentHashBalanceFactor = "consistentHashBalanceFactor"


This section is empty.


func NewAlgorithmProvider added in v0.10.168

func NewAlgorithmProvider() routing.PostProcessor

NewAlgorithmProvider creates a routing.PostProcessor used to initialize the algorithm of load balancing routes.


type Algorithm added in v0.11.29

type Algorithm int

Algorithm indicates the used load balancing algorithm.

const (
	// None is the default non-specified algorithm.
	None Algorithm = iota

	// RoundRobin indicates round-robin load balancing between the backend endpoints.

	// Random indicates random choice between the backend endpoints.

	// ConsistentHash indicates choice between the backends based on their hashed address.

	// PowerOfRandomNChoices selects N random endpoints and picks the one with least outstanding requests from them.

func AlgorithmFromString added in v0.11.29

func AlgorithmFromString(a string) (Algorithm, error)

AlgorithmFromString parses the string representation of the algorithm definition.

func (Algorithm) String added in v0.11.29

func (a Algorithm) String() string

String returns the string representation of an algorithm definition.

type HealthcheckPostProcessor

type HealthcheckPostProcessor struct{ *LB }

HealthcheckPostProcessor wraps the LB structure implementing the routing.PostProcessor interface for filtering healthy routes.

func (HealthcheckPostProcessor) Do

Do filters the routes with healthy backends.

type LB

type LB struct {
	// contains filtered or unexported fields

LB stores state of routes, which were reported dead or unhealthy by other packages, f.e. proxy. Based on reported routes LB starts to do active healthchecks to find if a route becomes haelthy again. Use NewLB() to create an LB

func New

func New(healthcheckInterval time.Duration) *LB

NewLB creates a new LB and starts background jobs for populating backends to check added routes and checking them every healthcheckInterval.

func (*LB) AddHealthcheck

func (lb *LB) AddHealthcheck(backend string)

AddHealthcheck can be used to report unhealthy routes, which loadbalancer will use to do active healthchecking and dataclients can ask the loadbalancer to filter unhealhyt or dead routes.

func (*LB) FilterHealthyMemberRoutes

func (lb *LB) FilterHealthyMemberRoutes(routes []*routing.Route) []*routing.Route

FilterHealthyMemberRoutes can be used by dataclients to filter for routes that have known not healthy backends.

Jump to

Keyboard shortcuts

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