balancer

package
v0.0.0-...-890cd7e Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2023 License: GPL-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	StrategyRandom         string = "random"
	StrategyRoundrobin     string = "roundrobin"
	StrategyConsistentHash string = "consistenthash"
)

Strategies

View Source
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

func (*AliveObjective) Sort

func (o *AliveObjective) Sort(all []*Node)

Sort implements Objective.

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

func (b *Balancer) InterfaceUpdated() error

InterfaceUpdated implements adapter.InterfaceUpdateListener

func (*Balancer) LogNodes

func (b *Balancer) LogNodes()

LogNodes logs all nodes status

func (*Balancer) Networks

func (b *Balancer) Networks() []string

Networks returns all networks supported by this balancer

func (*Balancer) Nodes

func (b *Balancer) Nodes(network string) []*Node

Nodes returns all Nodes for the network

func (*Balancer) Pick

func (b *Balancer) Pick(ctx context.Context, network string, destination M.Socksaddr) adapter.Outbound

Pick picks a node

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

func (*LeastObjective) Sort

func (o *LeastObjective) Sort(all []*Node)

Sort implements Objective.

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`.

  1. baseline: nil, expected: 0: selects top one node.
  2. baseline: nil, expected > 0: selects `expected` number of nodes.
  3. baselines: [...], expected > 0: select `expected` number of nodes, and also those near them according to baselines.
  4. 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

func (*Node) String

func (n *Node) String() string

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

type RoundRobinStrategy struct {
	sync.Mutex
	// contains filtered or unexported fields
}

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
)

func (Status) String

func (s Status) String() string

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

Jump to

Keyboard shortcuts

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