From 5b42f89e394a9e9fbd28fa93cc8a09186db0f233 Mon Sep 17 00:00:00 2001 From: Sameer Ajmani Date: Thu, 6 Jan 2022 13:57:05 -0500 Subject: 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 TryBot-Result: Gopher Robot Run-TryBot: Sameer Ajmani Auto-Submit: Sameer Ajmani --- api/next/51365.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 api/next/51365.txt (limited to 'api') diff --git a/api/next/51365.txt b/api/next/51365.txt new file mode 100644 index 0000000000..df629f1852 --- /dev/null +++ b/api/next/51365.txt @@ -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 -- cgit v1.2.1