-
Notifications
You must be signed in to change notification settings - Fork 17.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
Showing
4 changed files
with
231 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pkg context, func Cause(Context) error #51365 | ||
pkg context, func WithCancelCause(Context) (Context, CancelCauseFunc) #51365 | ||
pkg context, type CancelCauseFunc func(error) #51365 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters