conn

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2022 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package conn provides utilities related to connections.

Index

Constants

This section is empty.

Variables

View Source
var ErrConnectionUnavailable = errors.New("connection unavailable")

ErrConnectionUnavailable is returned by the Manager's Write method when the manager cannot yield a good connection.

Functions

func Exponential

func Exponential(d time.Duration) time.Duration

Exponential takes a duration and returns another one that is twice as long, +/- 50%. It is used to provide backoff for operations that may fail and should avoid thundering herds. See https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/ for rationale

Types

type AfterFunc

type AfterFunc func(time.Duration) <-chan time.Time

AfterFunc imitates time.After.

type Dialer

type Dialer func(network, address string) (net.Conn, error)

Dialer imitates net.Dial. Dialer is assumed to yield connections that are safe for use by multiple concurrent goroutines.

type Manager

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

Manager manages a net.Conn.

Clients provide a way to create the connection with a Dialer, network, and address. Clients should Take the connection when they want to use it, and Put back whatever error they receive from its use. When a non-nil error is Put, the connection is invalidated, and a new connection is established. Connection failures are retried after an exponential backoff.

func NewDefaultManager

func NewDefaultManager(network, address string, logger log.Logger) *Manager

NewDefaultManager is a helper constructor, suitable for most normal use in real (non-test) code. It uses the real net.Dial and time.After functions.

func NewManager

func NewManager(d Dialer, network, address string, after AfterFunc, logger log.Logger) *Manager

NewManager returns a connection manager using the passed Dialer, network, and address. The AfterFunc is used to control exponential backoff and retries. The logger is used to log errors; pass a log.NopLogger if you don't care to receive them. For normal use, prefer NewDefaultManager.

func (*Manager) Put

func (m *Manager) Put(err error)

Put accepts an error that came from a previously yielded connection. If the error is non-nil, the manager will invalidate the current connection and try to reconnect, with exponential backoff. Putting a nil error is a no-op.

func (*Manager) Take

func (m *Manager) Take() net.Conn

Take yields the current connection. It may be nil.

func (*Manager) Write

func (m *Manager) Write(b []byte) (int, error)

Write writes the passed data to the connection in a single Take/Put cycle.

Jump to

Keyboard shortcuts

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