multicontext

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2022 License: MIT Imports: 3 Imported by: 0

README

multicontext

One context.Context to rule them all.

multicontext is a go library providing a way to unify multiple context.Context into one. It supports all standard context features: values, cancellation, deadlines, timeouts.

It is useful in situations where you are not in charge of building the contexts that you will use, so you cannot derive one from the other.

For more information, see https://pkg.go.dev/github.com/nlm/go-multicontext

Documentation

Overview

multicontext package provides tools to aggregate the characteristics of multiple context.Context objects into one.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithContexts

func WithContexts(ctxs ...context.Context) context.Context

WithContexts creates a new context.Context from multiple child contexts.

Types

type MultiContext

type MultiContext struct {
	Ctxs []context.Context
}

A MultiContext is a struct providing context.Context and aggregating the characteritics of multiple child contexts.

func (MultiContext) Deadline

func (mc MultiContext) Deadline() (deadline time.Time, ok bool)

Deadline returns the time when work done on behalf of this context should be canceled. Deadline returns ok==false when no deadline is set. Successive calls to Deadline return the same results.

MultiContext returns the earliest deadline of all child contexts. If no child context have a deadline, it returns a zero-value for deadline, and ok==false

func (MultiContext) Done

func (mc MultiContext) Done() <-chan struct{}

Done returns a channel that's closed when work done on behalf of this context should be canceled. Done may return nil if this context can never be canceled. Successive calls to Done return the same value. The close of the Done channel may happen asynchronously, after the cancel function returns.

MultiContext returns a channel that will be closed if any of the channels of the child context is closed.

func (MultiContext) Err

func (mc MultiContext) Err() error

If Done is not yet closed, Err returns nil. If Done is closed, Err returns a non-nil error explaining why: Canceled if the context was canceled or DeadlineExceeded if the context's deadline passed. After Err returns a non-nil error, successive calls to Err return the same error.

MultiContext returns the error from the first child context that returns one. If no child context return an error, it returns nil.

func (MultiContext) Value

func (mc MultiContext) Value(key any) any

Value returns the value associated with this context for key, or nil if no value is associated with key. Successive calls to Value with the same key returns the same result.

MultiContext returns the value from the first child context that returns a non-nil value. If no child context returns a value, it returns nil.

Jump to

Keyboard shortcuts

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