contextutil

package
v0.0.0-...-1dc08c0 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2021 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetCancelReason

func GetCancelReason(ctx context.Context) error

GetCancelReason retrieves the cancel reason for a context that has been created via WithCancelReason. The reason will be nil if the context was not created with WithCancelReason, or if the context has not been canceled yet. Otherwise, the reason will be the error that the context's CancelWithReasonFunc was invoked with.

func RunWithTimeout

func RunWithTimeout(
	ctx context.Context, op string, timeout time.Duration, fn func(ctx context.Context) error,
) error

RunWithTimeout runs a function with a timeout, the same way you'd do with context.WithTimeout. It improves the opaque error messages returned by WithTimeout by augmenting them with the op string that is passed in.

func WithCancel

func WithCancel(parent context.Context) (context.Context, context.CancelFunc)

WithCancel adds an info log to context.WithCancel's CancelFunc. Prefer using WithCancelReason when possible.

Types

type CancelWithReasonFunc

type CancelWithReasonFunc func(reason error)

CancelWithReasonFunc is a context.CancelFunc that also passes along an error that is the reason for cancellation.

func WithCancelReason

func WithCancelReason(ctx context.Context) (context.Context, CancelWithReasonFunc)

WithCancelReason adds a CancelFunc to this context, returning a new cancellable context and a CancelWithReasonFunc, which is like context.CancelFunc, except it also takes a "reason" error. The context that is canceled with this CancelWithReasonFunc will immediately be updated to contain this "reason". The reason can be retrieved with GetCancelReason. This function doesn't change the deadline of a context if it already exists.

type TimeoutError

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

TimeoutError is a wrapped ContextDeadlineExceeded error. It indicates that an operation didn't complete within its designated timeout.

func (*TimeoutError) Cause

func (t *TimeoutError) Cause() error

Cause implements Causer.

func (*TimeoutError) Error

func (t *TimeoutError) Error() string

func (*TimeoutError) Format

func (t *TimeoutError) Format(s fmt.State, verb rune)

Format implements fmt.Formatter.

func (*TimeoutError) FormatError

func (t *TimeoutError) FormatError(p errors.Printer) error

FormatError implements errors.Formatter.

func (*TimeoutError) Temporary

func (*TimeoutError) Temporary() bool

Temporary implements net.Error.

func (*TimeoutError) Timeout

func (*TimeoutError) Timeout() bool

Timeout implements net.Error.

Jump to

Keyboard shortcuts

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