ctxtool

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2024 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 value that implements contex.Context is passed, then the value will be returned as is.

func FromChannel

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

FromChannel creates a new context from a channel.

func MergeCancellation

func MergeCancellation(parent, 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 canceller, 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(parent canceller, 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 canceller, 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.

type CancelContext added in v0.1.0

type CancelContext struct {
	context.Context
	// contains filtered or unexported fields
}

CancelContext holds a context with its corresponding CancelFunc. The cancel func will be called when calling `Cancel` on `CancelContext`.

Although it is discouraged to store context.Context in structs, sometimes it might be necessary to do so, because the cancellation needs to be called asynchronously, or an interface with a Stop method is required (context.Context is not part of the public API). One might still want to use a `context.Context` for shutdown handling and propagation in that case.

func WithCancelContext added in v0.1.0

func WithCancelContext(parent context.Context) CancelContext

WithCancelContext creates a new CancelContext similar to `context.WithCancel`. The `(CancelContext).Cancel()` method must be called in order to clean up associated resources.

func WrapCancel added in v0.1.0

func WrapCancel(parent context.Context, cancel context.CancelFunc) CancelContext

WrapCancel creates a CancelContext from an existing context and the associated cancel function.

Example:

cancelCtx := WrapCancel(context.WithCancel(parent))

func (CancelContext) Cancel added in v0.1.0

func (c CancelContext) Cancel()

Cancel calls the underlying cancel function, which marks the associated context as cancelled.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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