valueOnlyContext provides a utility to preserve only the values of a
Context. Suppressing any cancellation or deadline on that context being
propagated downstream of this value.
If preserveExpiredValues is false (default), and the valueCtx is canceled,
calls to lookup values with the Values method, will always return nil. Setting
preserveExpiredValues to true, will allow the valueOnlyContext to lookup
values in valueCtx even if valueCtx is canceled.
Based on the Go standard libraries net/lookup.go onlyValuesCtx utility.
https://github.com/golang/go/blob/da2773fe3e2f6106634673a38dc3a6eb875fe7d8/src/net/lookup.go
Contextcontext.ContextpreserveExpiredValuesboolvaluesCtxcontext.Context
Deadline returns the time when work done on behalf of this context
should be canceled. Deadline returns ok==false when no deadline is
set. Successive calls to Deadline return the same results.
Done returns a channel that's closed when work done on behalf of this
context should be canceled. Done may return nil if this context can
never be canceled. Successive calls to Done return the same value.
The close of the Done channel may happen asynchronously,
after the cancel function returns.
WithCancel arranges for Done to be closed when cancel is called;
WithDeadline arranges for Done to be closed when the deadline
expires; WithTimeout arranges for Done to be closed when the timeout
elapses.
Done is provided for use in select statements:
// Stream generates values with DoSomething and sends them to out
// until DoSomething returns an error or ctx.Done is closed.
func Stream(ctx context.Context, out chan<- Value) error {
for {
v, err := DoSomething(ctx)
if err != nil {
return err
}
select {
case <-ctx.Done():
return ctx.Err()
case out <- v:
}
}
}
See https://blog.golang.org/pipelines for more examples of how to use
a Done channel for cancellation.
If Done is not yet closed, Err returns nil.
If Done is closed, Err returns a non-nil error explaining why:
Canceled if the context was canceled
or DeadlineExceeded if the context's deadline passed.
After Err returns a non-nil error, successive calls to Err return the same error.
Value looks up the key, returning its value. If configured to not preserve
values of expired context, and the wrapping context is canceled, nil will be
returned.
*valueOnlyContext : context.Context
Package-Level Functions (total 3, all are exported)
GetPreserveExpiredValues looks up, and returns the PreserveExpressValues
value in the context. Returning true if enabled, false otherwise.
WithPreserveExpiredValues adds a Value to the Context if expired values
should be preserved, and looked up by a Context wrapped by
WithSuppressCancel.
WithPreserveExpiredValues must be added as a value to a Context, before that
Context is wrapped by WithSuppressCancel
WithSuppressCancel wraps the Context value, suppressing its deadline and
cancellation events being propagated downstream to consumer of the returned
context.
By default the wrapped Context's Values are available downstream until the
wrapped Context is canceled. Once the wrapped Context is canceled, Values
method called on the context return will no longer lookup any key. As they
are now considered expired.
To override this behavior, use WithPreserveExpiredValues on the Context
before it is wrapped by WithSuppressCancel. This will make the Context
returned by WithSuppressCancel allow lookup of expired values.
The pages are generated with Goldsv0.4.9. (GOOS=linux GOARCH=amd64)