balancer

package
v7.6.19 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2023 License: GPL-3.0 Imports: 15 Imported by: 0

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

View Source
const (
	// NetworkConnect is a pseudo network name to instruct the dialer to establish
	// a CONNECT tunnel to the proxy.
	NetworkConnect = "connect"
	// NetworkPersistent is a pseudo network name to instruct the dialer to
	// signal the proxy to establish a persistent HTTP connection over which
	// one or more HTTP requests can be sent directly.
	NetworkPersistent = "persistent"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Balancer

type Balancer struct {
	HasSucceedingDialer <-chan bool
	// contains filtered or unexported fields
}

Balancer balances connections among multiple Dialers.

func New

func New(allowBackgroundChecking func() bool, overallDialTimeout time.Duration, dialers ...Dialer) *Balancer

New creates a new Balancer using the supplied 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) DialContext

func (b *Balancer) DialContext(ctx context.Context, network, addr string) (net.Conn, error)

Dial dials (network, addr) using one of the currently active configured Dialers. The dialer is chosen based on the following ordering:

  • succeeding dialers are preferred to failing
  • dialers whose bandwidth is unknown are preferred to those whose bandwidth is known (in order to collect data)
  • faster dialers (based on bandwidth / RTT) are preferred to slower ones

Only Trusted Dialers are used to dial HTTP hosts.

Dial looks through the proxy connections based on the above ordering and dial with the first available. If none are available, it keeps cycling through the list in priority order until it finds one. It will keep trying for up to 30 seconds, at which point it gives up.

Blocks until dialers are available on the balancer (configured via the New constructor or b.Reset).

func (*Balancer) KeepLookingForSucceedingDialer

func (b *Balancer) KeepLookingForSucceedingDialer()

func (*Balancer) OnActiveDialer

func (b *Balancer) OnActiveDialer() <-chan Dialer

OnActiveDialer returns the channel of the last dialer the balancer was using. Can be called only once.

func (*Balancer) Reset

func (b *Balancer) Reset(dialers []Dialer)

Reset closes existing dialers and replaces them with new ones.

func (*Balancer) ResetFromExisting

func (b *Balancer) ResetFromExisting()

ResetFromExisting Resets using the existing dialers (useful when you want to force redialing).

type Dialer

type Dialer interface {
	// SupportsAddr indicates whether this Dialer supports the given addr. If it does not, the
	// balancer will not attempt to dial that addr with this Dialer.
	SupportsAddr(network, addr string) bool

	// DialContext dials out to the given origin. failedUpstream indicates whether
	// this was an upstream error (as opposed to errors connecting to the proxy).
	DialContext(ctx context.Context, network, addr string) (conn net.Conn, failedUpstream bool, err error)

	// Name returns the name for this Dialer
	Name() string

	// Label returns a label for this Dialer (includes Name plus more).
	Label() string

	// JustifiedLabel is like Label() but with elements justified for line-by
	// -line display.
	JustifiedLabel() string

	// Location returns the country code, country name and city name of the
	// dialer, in this order.
	Location() (string, string, string)

	// Protocol returns a string representation of the protocol used by this
	// Dialer.
	Protocol() string

	// Addr returns the address for this Dialer
	Addr() string

	// Trusted indicates whether or not this dialer is trusted
	Trusted() bool

	// NumPreconnecting returns the number of pending preconnect requests.
	NumPreconnecting() int

	// NumPreconnected returns the number of preconnected connections.
	NumPreconnected() int

	// MarkFailure marks a dial failure on this dialer.
	MarkFailure()

	// EstRTT provides a round trip delay time estimate, similar to how RTT is
	// estimated in TCP (https://tools.ietf.org/html/rfc6298)
	EstRTT() time.Duration

	// EstBandwidth provides the estimated bandwidth in Mbps
	EstBandwidth() float64

	// EstSuccessRate returns the estimated success rate dialing this dialer.
	EstSuccessRate() float64

	// Attempts returns the total number of dial attempts
	Attempts() int64

	// Successes returns the total number of dial successes
	Successes() int64

	// ConsecSuccesses returns the number of consecutive dial successes
	ConsecSuccesses() int64

	// Failures returns the total number of dial failures
	Failures() int64

	// ConsecFailures returns the number of consecutive dial failures
	ConsecFailures() int64

	// Succeeding indicates whether or not this dialer is currently good to use
	Succeeding() bool

	// Probe performs active probing of the proxy to better understand
	// connectivity and performance. If forPerformance is true, the dialer will
	// probe more and with bigger data in order for bandwidth estimation to
	// collect enough data to make a decent estimate. Probe returns true if it was
	// successfully able to communicate with the Proxy.
	Probe(forPerformance bool) bool
	// ProbeStats returns probe related stats for the dialer which can be used
	// to estimate the overhead of active probling.
	ProbeStats() (successes uint64, successKBs uint64, failures uint64, failedKBs uint64)

	// DataSent returns total bytes of application data sent to connections
	// created via this dialer.
	DataSent() uint64
	// DataRecv returns total bytes of application data received from
	// connections created via this dialer.
	DataRecv() uint64

	// Stop stops background processing for this Dialer.
	Stop()

	WriteStats(w io.Writer)
}

Dialer provides the ability to dial a proxy and obtain information needed to effectively load balance between dialers.

func SortDialers

func SortDialers(dialers []Dialer) []Dialer

Jump to

Keyboard shortcuts

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