ctxtool

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2020 License: Apache-2.0 Imports: 3 Imported by: 94

Documentation

Overview

Package ctxtool provides extra functionality and tools for use with context.Context.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromCanceller

func FromCanceller(c canceller) context.Context

FromCanceller creates a new context from a canceller. If a contex is passed, then Deadline and Value will be ignored.

func FromChannel

func FromChannel(ch <-chan struct{}) context.Context

FromChannel creates a new context from a channel.

func MergeCancellation

func MergeCancellation(ctx context.Context, other canceller) (context.Context, context.CancelFunc)

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

func MergeContexts(ctx1, ctx2 context.Context) (context.Context, context.CancelFunc)

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

func MergeDeadline(ctx context.Context, deadliner deadliner) context.Context

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

func MergeValues(ctx context.Context, overwrites valuer) context.Context

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

func WithChannel(parent context.Context, ch <-chan struct{}) (context.Context, context.CancelFunc)

WithChannel creates a context that is cancelled if the parent context is cancelled or if the given channel is closed.

func WithFunc

func WithFunc(ctx context.Context, fn func()) (context.Context, context.CancelFunc)

WithFunc creates a context that will execute the given function when the parent context gets cancelled.

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.

Jump to

Keyboard shortcuts

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