netutil

package
v1.9.4 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2023 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultLoopInterval = time.Millisecond * 50

Variables

View Source
var (
	ErrClosed = errors.New("dialer owner has been gone")
)

Functions

This section is empty.

Types

type BackgroundDialer

type BackgroundDialer struct {
	Dialer     *Dialer
	TaskGroup  *syncutil.TaskGroup
	TaskRunner *syncutil.TaskRunner
	// contains filtered or unexported fields
}

BackgroundDialer is a wrapper around Dialer that contains logic of glueing and cancellation of dial requests.

func (*BackgroundDialer) Cancel

func (d *BackgroundDialer) Cancel()

Cancel stops current background dial routine.

func (*BackgroundDialer) Dial

func (d *BackgroundDialer) Dial(ctx context.Context, cb func(net.Conn, error)) <-chan error

Dial begins dial routine if no one was started yet. It returns channel that signals about routine is done. If some routine was started before and not done yet, it returns done channel of that goroutine. It returns non-nil error only if dial routine was not started.

Started routine could be cancelled by calling Cancel method.

Note that cb is called only once. That is, if caller A calls Dial and caller B calls Dial immediately after, both of them will receive the same done channel, but only A's callback will be called in the end.

func (*BackgroundDialer) SetDeadline

func (d *BackgroundDialer) SetDeadline(t time.Time)

SetDeadline sets the dial deadline.

A deadline is an absolute time after which all dial routines fail. The deadline applies to all future and pending dials, not just the immediately following call to Dial. Cancelling some routine by calling Cancel method will not affect deadline. After a deadline has been exceeded, the dialer can be refreshed by setting a deadline in the future.

A zero value for t means dial routines will not time out.

func (*BackgroundDialer) SetDeadlineAtLeast

func (d *BackgroundDialer) SetDeadlineAtLeast(t time.Time) time.Time

SetDeadlineAtLeast sets the dial deadline if current deadline is zero or less than t. The other behaviour is the same as in SetDeadline.

A zero value for t is ignored.

It returns actual deadline value.

type Dialer

type Dialer struct {
	// Network and Addr are destination credentials.
	Network, Addr string

	// Timeout is the maximum amount of time a dial will wait for a single
	// connect to complete.
	Timeout time.Duration

	// LoopTimeout is the maximum amount of time a dial loop will wait for a
	// successful established connection. It may fail earlier if Closed option
	// is set or Cancel method is called.
	LoopTimeout time.Duration

	// LoopInterval is used to delay dial attepmts between each other.
	LoopInterval time.Duration

	// MaxLoopInterval is the maximum delay before next attempt to connect is
	// prepared. Note that LoopInterval is used as initial delay, and could be
	// increased by every dial attempt up to MaxLoopInterval.
	MaxLoopInterval time.Duration

	// Closed signals that Dialer owner is closed forever and will never want
	// to dial again.
	Closed chan struct{}

	// OnAttempt will be called with every dial attempt error. Nil error means
	// that dial succeed.
	OnAttempt func(error)

	// NetDial could be set to override dial function. By default net.Dial is
	// used.
	NetDial func(ctx context.Context, network, addr string) (net.Conn, error)

	// Logf could be set to receive log messages from Dialer.
	Logf   func(string, ...interface{})
	Debugf func(string, ...interface{})

	// DisableLogAddr removes addr part in log message prefix.
	DisableLogAddr bool
}

Dialer contains options for connecting to an address.

func (*Dialer) Dial

func (d *Dialer) Dial(ctx context.Context) (conn net.Conn, err error)

Dial tries to connect until some of events occur: - successful connect; – ctx is cancelled; – dialer owner is closed; – loop timeout exceeded (if set);

Jump to

Keyboard shortcuts

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