Version: v0.7.0 Latest Latest

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

Go to latest
Published: Dec 22, 2014 License: Apache-2.0, Apache-2.0 Imports: 11 Imported by: 0



Dynamic weighted round robin load balancer



View Source
const (
	// This is the maximum weight that handler will set for the endpoint
	FSMMaxWeight = 4096
	// Multiplier for the endpoint weight
	FSMGrowFactor = 16


This section is empty.


This section is empty.


type EndpointOptions

type EndpointOptions struct {
	// Relative weight for the enpoint to other enpoints in the load balancer
	Weight int

	// Meter tracks the failure count and is used to do failover
	Meter metrics.FailRateMeter

Set additional parameters for the endpoint can be supplied when adding endpoint

type EndpointWeight

type EndpointWeight struct {
	Endpoint *WeightedEndpoint
	Weight   int

func (*EndpointWeight) GetEndpoint

func (ew *EndpointWeight) GetEndpoint() *WeightedEndpoint

func (*EndpointWeight) GetWeight

func (ew *EndpointWeight) GetWeight() int

func (*EndpointWeight) SetWeight

func (ew *EndpointWeight) SetWeight(w int)

type FSMHandler

type FSMHandler struct {
	// contains filtered or unexported fields

This handler increases weights on endpoints that perform better than others it also rolls back to original weights if the endpoints have changed.

func NewFSMHandler

func NewFSMHandler() (*FSMHandler, error)

func NewFSMHandlerWithOptions

func NewFSMHandlerWithOptions(timeProvider timetools.TimeProvider) (*FSMHandler, error)

func (*FSMHandler) AdjustWeights

func (fsm *FSMHandler) AdjustWeights() ([]SuggestedWeight, error)

Called on every load balancer NextEndpoint call, returns the suggested weights on every call, can adjust weights if needed.

func (*FSMHandler) Init

func (fsm *FSMHandler) Init(endpoints []*WeightedEndpoint)

type FailureHandler

type FailureHandler interface {
	// Returns error if something bad happened, returns suggested weights
	AdjustWeights() ([]SuggestedWeight, error)
	// Initializes handler with current set of endpoints. Will be called
	// each time endpoints are added or removed from the load balancer
	// to give failure handler a chance to set it's itenral state
	Init(endpoints []*WeightedEndpoint)

type Options

type Options struct {
	// Control time in tests
	TimeProvider timetools.TimeProvider
	// Algorithm that reacts on the failures and can adjust weights
	FailureHandler FailureHandler

type RoundRobin

type RoundRobin struct {
	// contains filtered or unexported fields

Dynamic weighted round robin load balancer.

func NewRoundRobin

func NewRoundRobin() (*RoundRobin, error)

func NewRoundRobinWithOptions

func NewRoundRobinWithOptions(o Options) (*RoundRobin, error)

func (*RoundRobin) AddEndpoint

func (rr *RoundRobin) AddEndpoint(endpoint endpoint.Endpoint) error

func (*RoundRobin) AddEndpointWithOptions

func (r *RoundRobin) AddEndpointWithOptions(endpoint endpoint.Endpoint, options EndpointOptions) error

In case if endpoint is already present in the load balancer, returns error

func (*RoundRobin) FindEndpointById

func (r *RoundRobin) FindEndpointById(id string) *WeightedEndpoint

func (*RoundRobin) FindEndpointByUrl

func (r *RoundRobin) FindEndpointByUrl(url string) *WeightedEndpoint

func (*RoundRobin) GetEndpoints

func (r *RoundRobin) GetEndpoints() []*WeightedEndpoint

func (*RoundRobin) NextEndpoint

func (r *RoundRobin) NextEndpoint(req request.Request) (endpoint.Endpoint, error)

func (*RoundRobin) ObserveRequest

func (rr *RoundRobin) ObserveRequest(request.Request)

func (*RoundRobin) ObserveResponse

func (rr *RoundRobin) ObserveResponse(req request.Request, a request.Attempt)

func (*RoundRobin) ProcessRequest

func (rr *RoundRobin) ProcessRequest(request.Request) (*http.Response, error)

func (*RoundRobin) ProcessResponse

func (rr *RoundRobin) ProcessResponse(req request.Request, a request.Attempt)

func (*RoundRobin) RemoveEndpoint

func (r *RoundRobin) RemoveEndpoint(endpoint endpoint.Endpoint) error

type SuggestedWeight

type SuggestedWeight interface {
	GetEndpoint() *WeightedEndpoint
	GetWeight() int

type WeightedEndpoint

type WeightedEndpoint struct {
	// contains filtered or unexported fields

WeightedEndpoint wraps the endpoint and adds support for weights and failure detection.

func (*WeightedEndpoint) GetEffectiveWeight

func (we *WeightedEndpoint) GetEffectiveWeight() int

func (*WeightedEndpoint) GetId

func (we *WeightedEndpoint) GetId() string

func (*WeightedEndpoint) GetMeter

func (we *WeightedEndpoint) GetMeter() metrics.FailRateMeter

func (*WeightedEndpoint) GetOriginalEndpoint

func (we *WeightedEndpoint) GetOriginalEndpoint() endpoint.Endpoint

func (*WeightedEndpoint) GetOriginalWeight

func (we *WeightedEndpoint) GetOriginalWeight() int

func (*WeightedEndpoint) GetUrl

func (we *WeightedEndpoint) GetUrl() *url.URL

func (*WeightedEndpoint) String

func (we *WeightedEndpoint) String() string

type WeightedEndpoints

type WeightedEndpoints []*WeightedEndpoint

func (WeightedEndpoints) Len

func (we WeightedEndpoints) Len() int

func (WeightedEndpoints) Less

func (we WeightedEndpoints) Less(i, j int) bool

func (WeightedEndpoints) Swap

func (we WeightedEndpoints) Swap(i, j int)

Jump to

Keyboard shortcuts

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