loadbalancer

package
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2018 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package loadbalancer is client side load balancer

Index

Constants

View Source
const (
	StrategyRoundRobin        = "RoundRobin"
	StrategyRandom            = "Random"
	StrategySessionStickiness = "SessionStickiness"
	StrategyLatency           = "WeightedResponse"
	OperatorEqual             = "="
	OperatorGreater           = ">"
	OperatorSmaller           = "<"
	OperatorPattern           = "Pattern"
)

constant strings for load balance variables

View Source
const (
	ZoneAware = "zoneaware"
)

constant string for zoneaware

Variables

View Source
var (
	//ProtocolStatsMap saves all stats for all service's protocol, one protocol has a lot of instances
	ProtocolStatsMap = make(map[string][]*ProtocolStats)
	//maintain different locks since multiple goroutine access the map
	LatencyMapRWMutex sync.RWMutex
)

variables for latency map, rest and highway requests count

View Source
var (
	// ErrNoneAvailableInstance is to represent load balance error
	ErrNoneAvailableInstance = LBError{Message: "None available instance"}
)
View Source
var Filters = make(map[string]Filter)

Filters is a map of string and array of *registry.MicroServiceInstance

Functions

func BuildKey

func BuildKey(microServiceName, tags, protocol string) string

BuildKey return key of stats map

func CalculateAvgLatency

func CalculateAvgLatency()

CalculateAvgLatency Calculating the average latency for each instance using the statistics collected, key is addr/service/protocol

func DeleteSuccessiveFailureCount

func DeleteSuccessiveFailureCount(cookieValue string)

DeleteSuccessiveFailureCount deleting cookie from failure count map

func Enable

func Enable() error

Enable function is for to enable load balance strategy

func FilterAvailableZoneAffinity

func FilterAvailableZoneAffinity(old []*registry.MicroServiceInstance, c []*Criteria) []*registry.MicroServiceInstance

FilterAvailableZoneAffinity is a region and zone based Select Filter which will Do the selection of instance in the same region and zone, if not Do the selection of instance in any zone in same region , if not Do the selection of instance in any zone of any region

func FilterByMetadata

func FilterByMetadata(old []*registry.MicroServiceInstance, c []*Criteria) []*registry.MicroServiceInstance

FilterByMetadata filter instances based meta data

func GetStrategyPlugin

func GetStrategyPlugin(name string) (func() Strategy, error)

GetStrategyPlugin get strategy plugin

func GetSuccessiveFailureCount

func GetSuccessiveFailureCount(cookieValue string) int

GetSuccessiveFailureCount get failure count

func IncreaseSuccessiveFailureCount

func IncreaseSuccessiveFailureCount(cookieValue string)

IncreaseSuccessiveFailureCount increase failure count

func InstallFilter

func InstallFilter(name string, f Filter)

InstallFilter install filter

func InstallStrategy

func InstallStrategy(name string, s func() Strategy)

InstallStrategy install strategy

func ResetSuccessiveFailureMap

func ResetSuccessiveFailureMap()

ResetSuccessiveFailureMap make map again

func SetLatency

func SetLatency(latency time.Duration, addr, microServiceName string, tags utiltags.Tags, protocol string)

SetLatency for a instance ,it only save latest 10 stats for instance's protocol

func SortLatency

func SortLatency()

SortLatency sort instance based on the average latencies

Types

type ByDuration

type ByDuration []*ProtocolStats

ByDuration is for calculating the duration

func (ByDuration) Len

func (a ByDuration) Len() int

func (ByDuration) Less

func (a ByDuration) Less(i, j int) bool

func (ByDuration) Swap

func (a ByDuration) Swap(i, j int)

type Criteria

type Criteria struct {
	Key      string
	Operator string
	Value    string
}

Criteria is rule for filter

type Filter

type Filter func(instances []*registry.MicroServiceInstance, criteria []*Criteria) []*registry.MicroServiceInstance

Filter receive instances and criteria, it will filter instances based on criteria you defined,criteria is optional, you can give nil for it

type LBError

type LBError struct {
	Message string
}

LBError load balance error

func (LBError) Error

func (e LBError) Error() string

Error for to return load balance error message

type ProtocolStats

type ProtocolStats struct {
	Latency    []time.Duration
	Addr       string
	AvgLatency time.Duration
}

ProtocolStats store protocol stats

func (*ProtocolStats) CalculateAverageLatency

func (ps *ProtocolStats) CalculateAverageLatency()

CalculateAverageLatency make avg latency

func (*ProtocolStats) SaveLatency

func (ps *ProtocolStats) SaveLatency(l time.Duration)

SaveLatency save latest 10 record

type RandomStrategy

type RandomStrategy struct {
	// contains filtered or unexported fields
}

RandomStrategy is strategy

func (*RandomStrategy) Pick

Pick return instance

func (*RandomStrategy) ReceiveData

func (r *RandomStrategy) ReceiveData(instances []*registry.MicroServiceInstance, serviceName, protocol, sessionID string)

ReceiveData receive data

type RoundRobinStrategy

type RoundRobinStrategy struct {
	// contains filtered or unexported fields
}

RoundRobinStrategy is strategy

func (*RoundRobinStrategy) Pick

Pick return instance

func (*RoundRobinStrategy) ReceiveData

func (r *RoundRobinStrategy) ReceiveData(instances []*registry.MicroServiceInstance, serviceKey, protocol, sessionID string)

ReceiveData receive data

type SessionStickinessStrategy

type SessionStickinessStrategy struct {
	// contains filtered or unexported fields
}

SessionStickinessStrategy is strategy

func (*SessionStickinessStrategy) Pick

Pick return instance

func (*SessionStickinessStrategy) ReceiveData

func (r *SessionStickinessStrategy) ReceiveData(instances []*registry.MicroServiceInstance, serviceName, protocol, sessionID string)

ReceiveData receive data

type Strategy

type Strategy interface {
	ReceiveData(instances []*registry.MicroServiceInstance, serviceKey, protocol, sessionID string)
	Pick() (*registry.MicroServiceInstance, error)
}

Strategy is load balancer algorithm , call Pick to return one instance

func BuildStrategy

func BuildStrategy(consumerID, serviceName, protocol, sessionID string, fs []string,
	s Strategy, tags utiltags.Tags) (Strategy, error)

BuildStrategy query instance list and give it to Strategy then return Strategy

type WeightedResponseStrategy

type WeightedResponseStrategy struct {
	// contains filtered or unexported fields
}

WeightedResponseStrategy is a strategy plugin

func (*WeightedResponseStrategy) Pick

Pick return instance

func (*WeightedResponseStrategy) ReceiveData

func (r *WeightedResponseStrategy) ReceiveData(instances []*registry.MicroServiceInstance, serviceKey, protocol, sessionID string)

ReceiveData receive data

Jump to

Keyboard shortcuts

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