contextx

package
v1.13.0 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package contextx merges two context.Context values into one that looks up values in both and is cancelled when either one is.

Vendored from github.com/powerman/contextx (MIT licensed, same author) to avoid an external dependency for check's own [check.TB.MergeContext].

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Merge

func Merge(parent, extra context.Context) (ctx context.Context, cancel context.CancelFunc)

Merge returns a context that combines parent and extra: it looks up values in both (parent takes precedence) and is cancelled as soon as either parent or extra is cancelled, or the returned cancel is called. Its deadline is the earlier of parent's and extra's.

It is shorthand for MergeCancel(MergeValues(parent, extra), extra) and implements, for two contexts, the merge semantics of the rejected proposal https://github.com/golang/go/issues/36503.

Always call cancel to release resources once the merged context is no longer needed, even if it has already been cancelled by parent or extra.

func MergeCancel

func MergeCancel(parent, extra context.Context) (ctx context.Context, cancel context.CancelFunc)

MergeCancel returns a context derived from parent that is also cancelled when extra is cancelled, whichever happens first, or when the returned cancel is called. Values come solely from parent; extra contributes only its cancellation and deadline. The returned context's deadline is the earlier of parent's and extra's, and its cancellation context.Cause is the cause of whichever context cancelled it first.

MergeCancel uses context.AfterFunc, so it starts no goroutine. Always call cancel to release that registration once the merged context is no longer needed, even if it has already been cancelled by parent or extra.

func MergeValues

func MergeValues(parent context.Context, extra Values) context.Context

MergeValues returns a context that behaves exactly like parent but also looks up values in extra for keys absent in parent. Cancellation, deadline, and Err come solely from parent; extra contributes values only.

Because no cancellation is merged, MergeValues never starts a goroutine and has nothing to release, so it returns no cancel function.

Types

type Values

type Values interface {
	// Value returns the value associated with key, or nil if there is none.
	Value(key any) any
}

Values is an interface capturing only the value-lookup behavior of a context.Context. It lets MergeValues accept any value carrier, not just a full context.

Jump to

Keyboard shortcuts

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