Documentation ¶
Overview ¶
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 endpoints.
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, "http://127.0.0.1:9998", "http://127.0.0.1:9997">; r2: * -> <consistentHash, "http://127.0.0.1:9998", "http://127.0.0.1:9997">; r3: * -> <random, "http://127.0.0.1:9998", "http://127.0.0.1:9997">; r4: * -> <powerOfRandomNChoices, "http://127.0.0.1:9998", "http://127.0.0.1:9997">;
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 requests.
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.
Index ¶
Constants ¶
const ( ConsistentHashKey = "consistentHashKey" ConsistentHashBalanceFactor = "consistentHashBalanceFactor" )
Variables ¶
This section is empty.
Functions ¶
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.
Types ¶
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. RoundRobin // Random indicates random choice between the backend endpoints. Random // ConsistentHash indicates choice between the backends based on their hashed address. ConsistentHash // PowerOfRandomNChoices selects N random endpoints and picks the one with least outstanding requests from them. PowerOfRandomNChoices )
func AlgorithmFromString ¶ added in v0.11.29
AlgorithmFromString parses the string representation of the algorithm definition.
type HealthcheckPostProcessor ¶
type HealthcheckPostProcessor struct{ *LB }
HealthcheckPostProcessor wraps the LB structure implementing the routing.PostProcessor interface for filtering healthy routes.
type LB ¶
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 ¶
NewLB creates a new LB and starts background jobs for populating backends to check added routes and checking them every healthcheckInterval.
func (*LB) AddHealthcheck ¶
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.