balancer

package module
v0.0.0-...-70e3aff Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2016 License: Apache-2.0 Imports: 11 Imported by: 9

README

balancer Travis CI Status Coverage Status GoDoc

Connection balancer library for Go

To install:

go get github.com/getlantern/balancer

For docs:

godoc github.com/getlantern/balancer

===Benchmark

go test -bench . to evaluate performance of different strategy to pick dialer. Sticky and QualityFirst strategy seems has better result at this moment.

Example output:

BenchmarkQualityFirst-4   100000         23222 ns/op
--- BENCH: BenchmarkQualityFirst-4
    benchmark_test.go:98: '1%': 83/16850, '10%': 199/3596, '99%': 228/524,  fail/total = 510/20970 (2.4%) in 10000 runs
    benchmark_test.go:98: '1% 10ns±8ns': 85/18319, '10% 10ns±8ns': 86/1982, '50% 10ns±8ns': 85/457,  fail/total = 256/20758 (1.2%) in 10000 runs
    benchmark_test.go:98: '1%': 843/169828, '10%': 1784/35104, '99%': 3074/6879,  fail/total = 5701/211811 (2.7%) in 100000 runs
    benchmark_test.go:98: '1% 10ns±8ns': 955/184247, '10% 10ns±8ns': 954/19488, '50% 10ns±8ns': 954/4734,  fail/total = 2863/208469 (1.4%) in 100000 runs

Documentation

Overview

Package balancer provides load balancing of network connections per different strategies.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Fastest

func Fastest(dialers []*dialer) dialerHeap

Fastest strategy always pick the dialer with lowest recent average connect time

func QualityFirst

func QualityFirst(dialers []*dialer) dialerHeap

QualityFirst strategy behaves the same as Fastest strategy when both dialers are good recently, and falls back to Sticky strategy in other cases.

func Random

func Random(dialers []*dialer) dialerHeap

Random strategy gives even chance to each dialer, act as a baseline to other strategies.

func Sticky

func Sticky(dialers []*dialer) dialerHeap

Sticky strategy always pick the dialer with the biggest difference between consecutive successes and consecutive failures.

Types

type Balancer

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

Balancer balances connections among multiple Dialers.

func New

func New(st Strategy, dialers ...*Dialer) *Balancer

New creates a new Balancer using the supplied Strategy and Dialers.

func (*Balancer) Close

func (b *Balancer) Close()

Close closes this Balancer, stopping all background processing. You must call Close to avoid leaking goroutines.

func (*Balancer) Dial

func (b *Balancer) Dial(network, addr string) (net.Conn, error)

Dial dials (network, addr) using one of the currently active configured Dialers. The Dialer to choose depends on the Strategy when creating the balancer. Only Trusted Dialers are used to dial HTTP hosts.

If a Dialer fails to connect, Dial will keep trying at most 3 times until it either manages to connect, or runs out of dialers in which case it returns an error.

func (*Balancer) OnRequest

func (b *Balancer) OnRequest(req *http.Request)

OnRequest calls Dialer.OnRequest for every dialer in this balancer.

func (*Balancer) Reset

func (b *Balancer) Reset(dialers ...*Dialer)

Reset closes existing dialers and replaces them with new ones.

type Dialer

type Dialer struct {
	// Label: optional label with which to tag this dialer for debug logging.
	Label string

	// DialFN: this function dials the given network, addr.
	DialFN func(network, addr string) (net.Conn, error)

	// OnClose: (optional) callback for when this dialer is stopped.
	OnClose func()

	// Check: - a function that's used to test reachibility metrics
	// periodically or if the dialer was failed to connect.
	// It should return true for a successful check.
	//
	// Checks are scheduled at exponentially increasing intervals if dialer is
	// failed. Balancer will also schedule check when required.
	Check func() bool

	// Determines whether a dialer can be trusted with unencrypted traffic.
	Trusted bool

	// Modifies any HTTP requests made using connections from this dialer.
	OnRequest func(req *http.Request)
}

Dialer captures the configuration for dialing arbitrary addresses.

type Strategy

type Strategy func(dialers []*dialer) dialerHeap

Strategy determines the next dialer balancer will use given various metrics of each dialer.

func Weighted

func Weighted(ptQuality int, ptSpeed int) Strategy

TODO: still need to implement algorithm correctly. ptQuality: the percentage network quality contributes to total weight. the rest (100 - ptQuality) will be contributed by recent average connect time.

Jump to

Keyboard shortcuts

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