]> Cypherpunks.ru repositories - gostls13.git/commit
context: add APIs for writing and reading cancelation cause
authorSameer Ajmani <sameer@golang.org>
Thu, 6 Jan 2022 18:57:05 +0000 (13:57 -0500)
committerGopher Robot <gobot@golang.org>
Tue, 8 Nov 2022 13:51:16 +0000 (13:51 +0000)
commit5b42f89e394a9e9fbd28fa93cc8a09186db0f233
tree5c54ffff91741333a20485a6461e71e5082619da
parent0b7aa9fa5b207a3d9e2b1de2629babb6f5ce6e45
context: add APIs for writing and reading cancelation cause

Extend the context package to allow users to specify why a context was
canceled in the form of an error, the "cause". Users write the cause
by calling WithCancelCause to construct a derived context, then
calling cancel(cause) to cancel the context with the provided cause.
Users retrieve the cause by calling context.Cause(ctx), which returns
the cause of the first cancelation for ctx or any of its parents.

The cause is implemented as a field of cancelCtx, since only cancelCtx
can be canceled. Calling cancel copies the cause to all derived (child)
cancelCtxs. Calling Cause(ctx) finds the nearest parent cancelCtx by
looking up the context value keyed by cancelCtxKey.

API changes:
+pkg context, func Cause(Context) error
+pkg context, func WithCancelCause(Context) (Context, CancelCauseFunc)
+pkg context, type CancelCauseFunc func(error)

Fixes #26356
Fixes #51365

Change-Id: I15b62bd454c014db3f4f1498b35204451509e641
Reviewed-on: https://go-review.googlesource.com/c/go/+/375977
Reviewed-by: Damien Neil <dneil@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Sameer Ajmani <sameer@golang.org>
Auto-Submit: Sameer Ajmani <sameer@golang.org>
api/next/51365.txt [new file with mode: 0644]
src/context/context.go
src/context/context_test.go
src/context/x_test.go