netutil

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2019 License: CC0-1.0 Imports: 9 Imported by: 6

Documentation

Index

Constants

View Source
const MaxDialAttempts = 10

MaxDialAttempts is the maximum number of attempts RetryDial and RetryDialWithContext will make to dial a connection.

View Source
const MaxTemporaryAttempts = 5

MaxTemporaryAttempts is the maximum number of attempts RetryTemporary will make when encountering a temporary error.

View Source
const NilAddr = nilAddr(0)

NilAddr implements the net.Addr interface, and can be used to represent a nil address.

Variables

This section is empty.

Functions

func ClearDeadline

func ClearDeadline(c net.Conn) error

ClearDeadline clears the read/write deadline.

func ClearReadDeadline

func ClearReadDeadline(r SetReadDeadliner) error

ClearReadDeadline clears the read deadline.

func ClearWriteDeadline

func ClearWriteDeadline(w SetWriteDeadliner) error

ClearWriteDeadline clears the write deadline.

func IsTemporary

func IsTemporary(err error) bool

IsTemporary returns true if error is a temporary error

func IsTimeout

func IsTimeout(err error) bool

IsTimeout returns true if error is a connection timeout error

func LimitListener

func LimitListener(l net.Listener, n int) net.Listener

LimitListener returns a Listener based on l that accepts at most n simultaneous connections. It will panic if n is negative.

func ReadWriteCloserWithRollingDeadlines

func ReadWriteCloserWithRollingDeadlines(c net.Conn, rd time.Duration, wd time.Duration) io.ReadWriteCloser

ReadWriteCloserWithRollingDeadlines wraps the connection c and returns an io.ReadWriteCloser. Calls to Read on the returned io.ReadWriter have an idle timeout duration of rd; calls to Write on the returned io.ReadWriter have an idle timeout duration of wd.

func ReadWriterWithRollingDeadlines

func ReadWriterWithRollingDeadlines(c net.Conn, rd time.Duration, wd time.Duration) io.ReadWriter

ReadWriterWithRollingDeadlines wraps the connection c and returns an io.ReadWriter. Calls to Read on the returned io.ReadWriter have an idle timeout duration of rd; calls to Write on the returned io.ReadWriter have an idle timeout duration of wd.

func ReaderWithRollingDeadlines

func ReaderWithRollingDeadlines(c net.Conn, d time.Duration) io.Reader

ReaderWithRollingDeadlines wraps the connection c and returns an io.Reader. Calls to Read on the returned io.Reader have an idle timeout duration d.

func RetryDial

func RetryDial(ctx context.Context, dial DialFunc) (net.Conn, error)

RetryDial attempts to dial a connection using the given dial function. If the dial fails, it will be retried with exponential back-off. The exponential back-off caps out at approximately 1 minute. This will try MaxDialAttempts times before abandoning the attempt; you can use the context to cancel the attempt sooner.

func RetryDialWithContext

func RetryDialWithContext(ctx context.Context, dial DialWithContextFunc) (net.Conn, error)

RetryDialWithContext attempts to dial a connection using the given dial function. If the dial fails, it will be retried with exponential back-off. The exponential back-off caps out at approximately 1 minute. This will try MaxDialAttempts times before abandoning the attempt; you can use the context to cancel the attempt sooner.

func RetryTemporary

func RetryTemporary(f func() error) error

RetryTemporary executes the function f. If f returns a temporary error, it will be retried with exponential back-off. The exponential back-off caps out at approximately 1 second. This will try MaxTemporaryAttempts times before abandoning the attempt.

func SetDeadline

func SetDeadline(c net.Conn, d time.Duration) error

SetDeadline sets the read/write deadline using the given duration.

func SetDeadlineFromContext

func SetDeadlineFromContext(ctx context.Context, c net.Conn, d time.Duration) error

SetDeadlineFromContext sets the read/write deadline to the minimum of the given context's deadline or the given duration. If the context does not have a deadline, or if the context's deadline is 0, then the duration is used.

func SetReadDeadline

func SetReadDeadline(r SetReadDeadliner, d time.Duration) error

SetReadDeadline sets the read deadline using the given duration.

func SetReadDeadlineFromContext

func SetReadDeadlineFromContext(ctx context.Context, r SetReadDeadliner, d time.Duration) error

SetReadDeadlineFromContext sets the read deadline to the minimum of the given context's deadline or the given duration. If the context does not have a deadline, or if the context's deadline is 0, then the duration is used.

func SetWriteDeadline

func SetWriteDeadline(w SetWriteDeadliner, d time.Duration) error

SetWriteDeadline sets the write deadline using the given duration.

func SetWriteDeadlineFromContext

func SetWriteDeadlineFromContext(ctx context.Context, w SetWriteDeadliner, d time.Duration) error

SetWriteDeadlineFromContext sets the write deadline to the minimum of the given context's deadline or the given duration. If the context does not have a deadline, or if the context's deadline is 0, then the duration is used.

func WriterWithRollingDeadlines

func WriterWithRollingDeadlines(c net.Conn, d time.Duration) io.Writer

WriterWithRollingDeadlines wraps the connection c and returns an io.Writer. Calls to Write on the returned io.Writer have an idle timeout duration d.

Types

type Addrer

type Addrer interface {
	LocalAddr() net.Addr  // LocalAddr returns the local network address.
	RemoteAddr() net.Addr // RemoteAddr returns the remote network address.
}

Addrer is a subset of the net.Conn interface, only supporting methods for recovering the address.

type BinaryConn

BinaryConn is the interface encapsulating both the net.Conn and binaryutil.BinaryReadWriter interfaces.

func NewBinaryConn

func NewBinaryConn(c net.Conn, e binary.ByteOrder) BinaryConn

NewBinaryConn wraps the given net.Conn and binary.ByteOrder as a BinaryConn.

type BinaryConnReadWriteCloser

BinaryConnReadWriteCloser is the interface encapsulating both the ConnReadWriteCloser and binaryutil.BinaryReadWriter interfaces.

func NewBinaryConnReadWriteCloser

func NewBinaryConnReadWriteCloser(c ConnReadWriteCloser, e binary.ByteOrder) BinaryConnReadWriteCloser

NewBinaryConnReadWriteCloser wraps the given ConnReadWriteCloser and binary.ByteOrder as a BinaryConnReadWriteCloser.

type BinaryConnReader

type BinaryConnReader interface {
	ConnReader
	binaryutil.ByteOrderer
	binaryutil.BasicReader
}

BinaryConnReader is a subset of the BinaryConn interface, only supporting methods for reading data.

type BinaryConnWriter

type BinaryConnWriter interface {
	ConnWriter
	binaryutil.ByteOrderer
	binaryutil.BasicWriter
}

BinaryConnWriter is a subset of the BinaryConn interface, only supporting methods for writing data.

type ConnReadWriteCloser

type ConnReadWriteCloser interface {
	net.Conn
	CloseRead() error  // CloseRead shuts down the reading side of the connection. Most callers should just use Close.
	CloseWrite() error // CloseWrite shuts down the writing side of the connection. Most callers should just use Close.
}

ConnReadWriteCloser extends the net.Conn interface to include separate CloseRead and CloseWrite methods.

type ConnReader

type ConnReader interface {
	io.Reader
	SetReadDeadliner
	Addrer
}

ConnReader is a subset of the net.Conn interface, only supporting methods for reading data.

type ConnWriter

type ConnWriter interface {
	io.Writer
	SetWriteDeadliner
	Addrer
}

ConnWriter is a subset of the net.Conn interface, only supporting methods for writing data.

type DialFunc

type DialFunc func() (net.Conn, error)

DialFunc defines a function that can be used to dial a connection.

type DialWithContextFunc

type DialWithContextFunc func(ctx context.Context) (net.Conn, error)

DialWithContextFunc defines a function that can be used to dial a connection.

func DefaultTCPDialWithContextFunc

func DefaultTCPDialWithContextFunc(host string, port int, lg log.Interface) DialWithContextFunc

DefaultTCPDialWithContextFunc returns a DialWithContextFunc that calls

net.Dial("tcp", host + ":" + port)

Logging messages will be sent to lg, if non-nil.

type SetReadDeadliner

type SetReadDeadliner interface {
	SetReadDeadline(t time.Time) error // SetReadDeadline sets the deadline for future Read calls and any currently-blocked Read call. A zero value for t means Read will not time out.
}

SetReadDeadliner is the interface satisfied by the SetReadDeadline method.

type SetWriteDeadliner

type SetWriteDeadliner interface {
	SetWriteDeadline(t time.Time) error // SetWriteDeadline sets the deadline for future Write calls and any currently-blocked Write call. Even if write times out, it may return n > 0, indicating that some of the data was successfully written. A zero value for t means Write will not time out.
}

SetWriteDeadliner is the interface satisfied by the SetWriteDeadline method.

Jump to

Keyboard shortcuts

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