dialer

package
v7.6.175 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2024 License: GPL-3.0 Imports: 17 Imported by: 4

Documentation

Overview

Package dialer contains the interfaces for creating connections to proxies. It is designed to first connect as quickly as possible, and then to optimize for bandwidth and latency based on the proxies that are accessible. It does this by first using a connect-time based strategy to quickly find a working proxy, and then by using a multi-armed bandit strategy to optimize for bandwidth and latency amongst the proxies that are accessible.

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 Dialer

type Dialer interface {

	// DialContext dials out to the domain or IP address representing a destination site.
	DialContext(ctx context.Context, network, addr string) (net.Conn, error)

	// Close closes the dialer and cleans up any resources
	Close()
}

Dialer is the interface for creating connections destination sites. This is sent over a proxy connection, with the proxy handling the actual connection to the destination site. This represents a "Dialer" at the highest level in the sense that a Dialer is not tied to any specific proxy, but rather represents a higher level abstraction that can be used to dial any proxy depending on optimization strategies.

func New

func New(opts *Options) Dialer

New creates a new dialer that first tries to connect as quickly as possilbe while also optimizing for the fastest dialer.

func NewBandit

func NewBandit(opts *Options) (Dialer, error)

NewBandit creates a new bandit given the available dialers and options with callbacks to be called when a dialer is selected, an error occurs, etc.

func NewTwoPhaseDialer

func NewTwoPhaseDialer(opts *Options, next func(opts *Options, existing Dialer) Dialer) Dialer

NewTwoPhaseDialer creates a new dialer for checking proxy connectivity.

func NoDialer

func NoDialer() Dialer

NoDialer returns a dialer that does nothing. This is useful during startup when we don't yet have proxies to dial through.

type Options

type Options struct {
	// The available dialers to use when creating a new dialer
	Dialers []ProxyDialer

	// OnError is the onError callback that is called when the dialer encounters a dial error
	OnError func(error, bool)

	// OnSuccess is the callback that is called by dialer after a successful dial.
	OnSuccess func(ProxyDialer)

	// BanditDir is the directory where the bandit will store its data
	BanditDir string
}

Options are the options used to create a new bandit

func (*Options) Clone

func (o *Options) Clone() *Options

Clone creates a deep copy of the Options object

type ProxyDialer

type ProxyDialer interface {

	// DialProxy dials the proxy but does not yet dial the origin.
	DialProxy(ctx context.Context) (net.Conn, error)

	// 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

	// 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

	// Ready returns a channel which will have a value on it only when initialization
	// of the dialer is complete. If initialization failed, the channel will have a non-nil
	// error value. If initialization is not required, this will return nil channel value.
	Ready() <-chan error

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

	WriteStats(w io.Writer)
}

ProxyDialer provides the ability to dial a proxy and obtain information needed to record performance data about proxies.

Jump to

Keyboard shortcuts

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