| 1 | // Package contextx merges two [context.Context] values into one |
| 2 | // that looks up values in both and is cancelled when either one is. |
| 3 | // |
| 4 | // Vendored from github.com/powerman/contextx (MIT licensed, same author) |
| 5 | // to avoid an external dependency for check's own [check.TB.MergeContext]. |
| 6 | package contextx |
| 7 | |
| 8 | import ( |
| 9 | "context" |
| 10 | "time" |
| 11 | ) |
| 12 | |
| 13 | // Values is an interface capturing only the value-lookup behavior of a [context.Context]. |
| 14 | // It lets [MergeValues] accept any value carrier, not just a full context. |
| 15 | type Values interface { |
| 16 | // Value returns the value associated with key, or nil if there is none. |
| 17 | Value(key any) any |
| 18 | } |
| 19 | |
| 20 | // Merge returns a context that combines parent and extra: |
| 21 | // it looks up values in both (parent takes precedence) |
| 22 | // and is cancelled as soon as either parent or extra is cancelled, |
| 23 | // or the returned cancel is called. |
| 24 | // Its deadline is the earlier of parent's and extra's. |
| 25 | // |
| 26 | // It is shorthand for MergeCancel(MergeValues(parent, extra), extra) |
| 27 | // and implements, for two contexts, the merge semantics of the rejected proposal |
| 28 | // https://github.com/golang/go/issues/36503. |
| 29 | // |
| 30 | // Always call cancel to release resources once the merged context is no longer needed, |
| 31 | // even if it has already been cancelled by parent or extra. |
| 32 | func Merge(parent, extra context.Context) (ctx context.Context, cancel context.CancelFunc) { |
| 33 | return MergeCancel(MergeValues(parent, extra), extra) |
| 34 | } |
| 35 | |
| 36 | type mergedValues struct { |
| 37 | context.Context |
| 38 | |
| 39 | extra Values |
| 40 | } |
| 41 | |
| 42 | // MergeValues returns a context that behaves exactly like parent |
| 43 | // but also looks up values in extra for keys absent in parent. |
| 44 | // Cancellation, deadline, and Err come solely from parent; extra contributes values only. |
| 45 | // |
| 46 | // Because no cancellation is merged, MergeValues never starts a goroutine |
| 47 | // and has nothing to release, so it returns no cancel function. |
| 48 | func MergeValues(parent context.Context, extra Values) context.Context { |
| 49 | return &mergedValues{Context: parent, extra: extra} |
| 50 | } |
| 51 | |
| 52 | func (c *mergedValues) Value(key any) any { |
| 53 | if v := c.Context.Value(key); v != nil { |
| 54 | return v |
| 55 | } |
| 56 | return c.extra.Value(key) |
| 57 | } |
| 58 | |
| 59 | type mergedCancel struct { |
| 60 | context.Context |
| 61 | |
| 62 | extra context.Context |
| 63 | } |
| 64 | |
| 65 | // MergeCancel returns a context derived from parent that is also cancelled |
| 66 | // when extra is cancelled, whichever happens first, or when the returned cancel is called. |
| 67 | // Values come solely from parent; extra contributes only its cancellation and deadline. |
| 68 | // The returned context's deadline is the earlier of parent's and extra's, |
| 69 | // and its cancellation [context.Cause] is the cause of whichever context cancelled it first. |
| 70 | // |
| 71 | // MergeCancel uses [context.AfterFunc], so it starts no goroutine. |
| 72 | // Always call cancel to release that registration once the merged context is no longer needed, |
| 73 | // even if it has already been cancelled by parent or extra. |
| 74 | func MergeCancel(parent, extra context.Context) (ctx context.Context, cancel context.CancelFunc) { |
| 75 | merged, cancelCause := context.WithCancelCause(parent) |
| 76 | stop := context.AfterFunc(extra, func() { |
| 77 | cancelCause(context.Cause(extra)) |
| 78 | }) |
| 79 | return &mergedCancel{Context: merged, extra: extra}, func() { |
| 80 | stop() |
| 81 | cancelCause(context.Canceled) |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | func (c *mergedCancel) Deadline() (deadline time.Time, ok bool) { |
| 86 | d1, ok1 := c.Context.Deadline() |
| 87 | d2, ok2 := c.extra.Deadline() |
| 88 | switch { |
| 89 | case !ok2: |
| 90 | return d1, ok1 |
| 91 | case !ok1: |
| 92 | return d2, ok2 |
| 93 | case d2.Before(d1): |
| 94 | return d2, true |
| 95 | default: |
| 96 | return d1, ok1 |
| 97 | } |
| 98 | } |