Documentation ¶
Index ¶
Constants ¶
const ( StrategyRandom string = "random" StrategyRoundrobin string = "roundrobin" StrategyConsistentHash string = "consistenthash" )
Strategies
const ( ObjectiveAlive string = "alive" ObjectiveQualified string = "qualified" ObjectiveLeastPing string = "leastping" ObjectiveLeastLoad string = "leastload" )
Objectives
Variables ¶
This section is empty.
Functions ¶
func SortByLeast ¶
func SortByLeast(nodes []*Node, rttFunc func(*Node) healthcheck.RTT)
SortByLeast sorts nodes by least value from rttFunc and more.
Types ¶
type AliveObjective ¶
type AliveObjective struct{}
AliveObjective is the alive nodes objective
func NewAliveObjective ¶
func NewAliveObjective() *AliveObjective
NewAliveObjective returns a new AliveObjective
func (*AliveObjective) Filter ¶
func (o *AliveObjective) Filter(all []*Node) []*Node
Filter implements Objective. NOTICE: be aware of the coding convention of this function
type Balancer ¶
type Balancer struct { *healthcheck.HealthCheck // contains filtered or unexported fields }
Balancer is the load balancer
func New ¶
func New( router adapter.Router, providers []adapter.Provider, providersByTag map[string]adapter.Provider, options *option.LoadBalanceOutboundOptions, logger log.ContextLogger, ) (*Balancer, error)
New creates a new load balancer
The globalHistory is optional and is only used to sync latency history between different health checkers. Each HealthCheck will maintain its own history storage since different ones can have different check destinations, sampling numbers, etc.
func (*Balancer) InterfaceUpdated ¶
InterfaceUpdated implements adapter.InterfaceUpdateListener
type ConsistentHashStrategy ¶
type ConsistentHashStrategy struct{}
ConsistentHashStrategy is the random strategy
func NewConsistentHashStrategy ¶
func NewConsistentHashStrategy() *ConsistentHashStrategy
NewConsistentHashStrategy returns a new ConsistentHashStrategy
func (*ConsistentHashStrategy) Pick ¶
func (s *ConsistentHashStrategy) Pick(all, filtered []*Node, metadata *adapter.InboundContext) *Node
Pick implements Strategy
type LeastObjective ¶
type LeastObjective struct { *QualifiedObjective // contains filtered or unexported fields }
LeastObjective is the least load / least ping balancing objective
func NewLeastObjective ¶
func NewLeastObjective(sampling uint, options option.LoadBalancePickOptions, rttFunc func(node *Node) healthcheck.RTT) *LeastObjective
NewLeastObjective returns a new LeastObjective
func (*LeastObjective) Filter ¶
func (o *LeastObjective) Filter(all []*Node) []*Node
Filter implements Objective. NOTICE: be aware of the coding convention of this function
type Node ¶
type Node struct { adapter.Outbound healthcheck.Stats Index int Status Status // contains filtered or unexported fields }
Node is a banalcer Node with health check result
func LeastNodes ¶
func LeastNodes( nodes []*Node, expected int, baselines []healthcheck.RTT, rttFunc func(node *Node) healthcheck.RTT, ) []*Node
LeastNodes filters ordered nodes according to `baselines` and `expected`.
- baseline: nil, expected: 0: selects top one node.
- baseline: nil, expected > 0: selects `expected` number of nodes.
- baselines: [...], expected > 0: select `expected` number of nodes, and also those near them according to baselines.
- baselines: [...], expected <= 0: go through all baselines until find selects, if not, select the top one.
func (*Node) CalcStatus ¶
func (n *Node) CalcStatus(maxRTT healthcheck.RTT, maxFailRate float32)
CalcStatus calculates & updates the status of the node according to the healthcheck statistics
type Objective ¶
type Objective interface { // Filter filters nodes from all. // // Conventions: // 1. keeps the slice `all` unchanged, because the number and order matters for consistent hash strategy. // 2. takes care of the fallback logic, it never returns an empty slice if `all` is not empty. Filter(all []*Node) []*Node // Sort sorts nodes according to the objective, better nodes are in front. Sort([]*Node) }
Objective is the interface for balancer objectives
type QualifiedObjective ¶
type QualifiedObjective struct {
*AliveObjective
}
QualifiedObjective is the qualified balancing objective
func NewQualifiedObjective ¶
func NewQualifiedObjective() *QualifiedObjective
NewQualifiedObjective returns a new QualifiedObjective
func (*QualifiedObjective) Filter ¶
func (o *QualifiedObjective) Filter(all []*Node) []*Node
Filter implements Objective. NOTICE: be aware of the coding convention of this function
func (*QualifiedObjective) Sort ¶
func (o *QualifiedObjective) Sort(all []*Node)
Sort implements Objective.
type RandomStrategy ¶
type RandomStrategy struct{}
RandomStrategy is the random strategy
func NewRandomStrategy ¶
func NewRandomStrategy() *RandomStrategy
NewRandomStrategy returns a new RandomStrategy
func (*RandomStrategy) Pick ¶
func (s *RandomStrategy) Pick(_, filtered []*Node, _ *adapter.InboundContext) *Node
Pick implements Strategy
type RoundRobinStrategy ¶
RoundRobinStrategy is the round robin strategy
func NewRoundRobinStrategy ¶
func NewRoundRobinStrategy() *RoundRobinStrategy
NewRoundRobinStrategy returns a new RoundRobinStrategy
func (*RoundRobinStrategy) Pick ¶
func (s *RoundRobinStrategy) Pick(_, filtered []*Node, _ *adapter.InboundContext) *Node
Pick implements Strategy
type Status ¶
type Status int
Status is the status of a node
const ( // StatusDead is the status of a node that is dead StatusDead Status = iota // StatusUnknown is the status of a node that is not tested yet StatusUnknown // StatusAlive is the status of a node that is alive StatusAlive // StatusQualified is the status of a node that is qualified StatusQualified )
type Strategy ¶
type Strategy interface { // Pick picks a node from the given nodes // // - The `all` nodes are stable in both number and order between pickings. // - The `filtered` nodes are filtered and sorted by the objective. Pick(all, filtered []*Node, metadata *adapter.InboundContext) *Node }
Strategy is the interface for balancer strategies