cext

package
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: May 24, 2023 License: MIT Imports: 4 Imported by: 0

README

Context Extension

This package offers extra functionality we need from contexts that is not available in the standard context package.

Functions

func Detach
// Detach returns a context that keeps all values of the parent context
// but detaches from its cancellation and error handling.
func Detach(ctx context.Context) context.Context

This function comes in handy when some code needs to follow through to completion instead of getting cancelled halfway by the parent context (e.g. cancelled by)

func Delegate
// Delegate returns a context that keeps all values of the valueCtx while
// taking its cancellation signal and error from the cancelCtx.
func Delegate(cancelCtx context.Context, valueCtx context.Context) context.Context

The standard Context in Go is meant for 2 distinct purposes: carrying request-level data and looking out for cancelling signals. In cases where we want to delegate these responsibilities to 2 different Contexts, this function will get the job done.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Delegate added in v1.0.2

func Delegate(cancelCtx context.Context, valueCtx context.Context) context.Context

Delegate returns a context that keeps all values of the valueCtx while taking its cancellation signal and error from the cancelCtx.

func Detach

func Detach(ctx context.Context) context.Context

Detach returns a context that keeps all values of the parent context but detaches from its cancellation and error handling.

func WithAcyclicBreadcrumb added in v1.0.3

func WithAcyclicBreadcrumb[V comparable](ctx context.Context, breadcrumbID V) (context.Context, bool)

WithAcyclicBreadcrumb return a new context with the given breadcrumbID embedded inside and true if this ID has never been encountered in the execution path before. Otherwise, it returns a nil context.Context and false to indicate the execution is running in circle.

Note: the provided breadcrumbID must be comparable and should not be of type string or any other built-in type to avoid collisions between packages using this context. You should define your own types for breadcrumbID similar to the best practices for using context.WithValue.

Types

This section is empty.

Jump to

Keyboard shortcuts

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