Documentation
¶
Overview ¶
Package ctxtool provides extra functionality and tools for use with context.Context.
Index ¶
- func FromCanceller(c canceller) context.Context
- func FromChannel(ch <-chan struct{}) context.Context
- func MergeCancellation(ctx context.Context, other canceller) (context.Context, context.CancelFunc)
- func MergeContexts(ctx1, ctx2 context.Context) (context.Context, context.CancelFunc)
- func MergeDeadline(ctx context.Context, deadliner deadliner) context.Context
- func MergeValues(ctx context.Context, overwrites valuer) context.Context
- func WithChannel(parent context.Context, ch <-chan struct{}) (context.Context, context.CancelFunc)
- func WithFunc(ctx context.Context, fn func()) (context.Context, context.CancelFunc)
- type AutoCancel
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FromCanceller ¶
FromCanceller creates a new context from a canceller. If a contex is passed, then Deadline and Value will be ignored.
func FromChannel ¶
FromChannel creates a new context from a channel.
func MergeCancellation ¶
MergeCancellation creates a new context that will be cancelled if one of the two input contexts gets canceled. The `Values` and `Deadline` are taken from the first context.
func MergeContexts ¶
MergeContexts merges cancellation and values of 2 contexts. The resulting context is canceled by the first context that got canceled. The ctx2 overwrites values in ctx1 during value lookup.
func MergeDeadline ¶
MergeDeadline merges the deadline of two contexts. The resulting context deadline will be the lesser deadline between the two context. If neither context configures a deadline, the original context is returned.
func MergeValues ¶
MergeValues merges the values from ctx and overwrites. Value lookup will occur on `overwrites` first. Deadline and cancellation are still driven by the first context. In order to merge cancellation use MergeCancellation.
func WithChannel ¶
WithChannel creates a context that is cancelled if the parent context is cancelled or if the given channel is closed.
Types ¶
type AutoCancel ¶
type AutoCancel struct {
// contains filtered or unexported fields
}
AutoCancel collects cancel functions to be executed at the end of the function scope.
Example:
var ac AutoCancel defer ac.Cancel() ctx := ac.With(context.WithCancel(context.Background())) ctx := ac.With(context.WithTimeout(ctx, 5 * time.Second)) ... // do something with ctx
func (*AutoCancel) Add ¶
func (ac *AutoCancel) Add(fn context.CancelFunc)
Add adds a new cancel function to the AutoCancel. The function will be run before any other already registered cancel function.
func (*AutoCancel) Cancel ¶
func (ac *AutoCancel) Cancel()
Cancel calls all registered cancel functions in reverse order.
func (*AutoCancel) With ¶
func (ac *AutoCancel) With(ctx context.Context, cancel context.CancelFunc) context.Context
With is used to wrap a Context constructer call that returns a context and a cancel function. The cancel function is automatically added to AutoCancel and the original context is returned as is.