Documentation
¶
Overview ¶
Package context is a drop in replacement for the standard library's context package. It provides additional functionality to the context package, such as attaching various clients to the context when calling Background(). This should be called after init.Service() to ensure that the clients are initialized. All clients that are attached to the context should return a a value that is safe to call methods on.
Index ¶
- Variables
- func AfterFunc(ctx Context, f func()) (stop func() bool)
- func Cause(c Context) error
- func Log(ctx Context) *log.Logger
- func Meter(ctx Context, opts ...metric.MeterOption) metric.Meter
- func MeterProvider(ctx Context) metric.MeterProvider
- func MeterWithStackFrame(ctx Context, sf uint, opts ...metric.MeterOption) metric.Meter
- func Pool(ctx Context) *worker.Pool
- func SetShouldTrace(ctx context.Context, b bool) context.Context
- func ShouldTrace(ctx context.Context) bool
- func Tasks(ctx Context) *background.Tasks
- func WithCancel(parent Context) (ctx Context, cancel CancelFunc)
- func WithCancelCause(parent Context) (ctx Context, cancel CancelCauseFunc)
- func WithDeadline(parent Context, d time.Time) (Context, CancelFunc)
- func WithDeadlineCause(parent Context, d time.Time, cause error) (Context, CancelFunc)
- func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc)
- func WithTimeoutCause(parent Context, timeout time.Duration, cause error) (Context, CancelFunc)
- type CancelCauseFunc
- type CancelFunc
- type Context
Constants ¶
This section is empty.
Variables ¶
var ( // Canceled is the error returned by [Context.Err] when the context is canceled. Canceled = context.Canceled // DeadlineExceeded is the error returned by [Context.Err] when the context's deadline passes. DeadlineExceeded = context.DeadlineExceeded )
Functions ¶
func AfterFunc ¶
AfterFunc arranges to call f in its own goroutine after ctx is done (canceled or timed out). If ctx is already done, AfterFunc calls f immediately in its own goroutine.
Multiple calls to AfterFunc on a context operate independently; one does not replace another.
Calling the returned stop function stops the association of ctx with f. It returns true if the call stopped f from being run. If stop returns false, either the context is done and f has been started in its own goroutine; or f was already stopped. The stop function does not wait for f to complete before returning. If the caller needs to know whether f is completed, it must coordinate with f explicitly.
If ctx has a "AfterFunc(func()) func() bool" method, AfterFunc will use it to schedule the call.
func Cause ¶
Cause returns a non-nil error explaining why c was canceled. The first cancellation of c or one of its parents sets the cause. If that cancellation happened via a call to CancelCauseFunc(err), then Cause returns err. Otherwise Cause(c) returns the same value as c.Err(). Cause returns nil if c has not been canceled yet.
func Log ¶
Log returns the logger attached to the context. If no logger is attached, it returns log.Default().
func Meter ¶
func Meter(ctx Context, opts ...metric.MeterOption) metric.Meter
Meter returns a metric.Meter scoped to the package that calls context.Meter(). If you need to have a sub-namespace for a specific package, you should use the MeterProvider() function to get the meter provider. If no meter is attached to the context it returns a meter from metrics.Default(). This may be a noop Meter.
func MeterProvider ¶
func MeterProvider(ctx Context) metric.MeterProvider
MeterProvider returns a metric.MeterProvider attached to the context. If no meter provider is attached, it returns metrics.Default(). This may be a noop provider.
func MeterWithStackFrame ¶
MeterWithStackFrame returns a metric.Meter scoped to the stack frame number provided by "sf". This is for uses by packages that use this underneath so they can get the write stack frame. Generally, you should be using Meter().
func Pool ¶
Pool returns the worker pool attached to the context. If no pool is attached, it returns worker.Default().
func SetShouldTrace ¶
SetShouldTrace attaches a boolean value to the context to indicate if the request should be traced. This is not usually used by a service, but by the middleware to determine if the request should be traced. This only works if done before the trace is started.
func ShouldTrace ¶
ShouldTrace returns true if the request has had SetShouldTrace called on it.
func Tasks ¶
func Tasks(ctx Context) *background.Tasks
Tasks returns a background.Tasks attached to the context. If not tasks are attached, it returns background.Default().
func WithCancel ¶
func WithCancel(parent Context) (ctx Context, cancel CancelFunc)
WithCancel returns a copy of parent with a new Done channel. The returned context's Done channel is closed when the returned cancel function is called or when the parent context's Done channel is closed, whichever happens first.
Canceling this context releases resources associated with it, so code should call cancel as soon as the operations running in this Context complete.
func WithCancelCause ¶
func WithCancelCause(parent Context) (ctx Context, cancel CancelCauseFunc)
WithCancelCause behaves like WithCancel but returns a CancelCauseFunc instead of a CancelFunc. Calling cancel with a non-nil error (the "cause") records that error in ctx; it can then be retrieved using Cause(ctx). Calling cancel with nil sets the cause to Canceled.
Example use:
ctx, cancel := context.WithCancelCause(parent) cancel(myError) ctx.Err() // returns context.Canceled context.Cause(ctx) // returns myError
func WithDeadline ¶
func WithDeadline(parent Context, d time.Time) (Context, CancelFunc)
WithDeadline returns a copy of the parent context with the deadline adjusted to be no later than d. If the parent's deadline is already earlier than d, WithDeadline(parent, d) is semantically equivalent to parent. The returned [Context.Done] channel is closed when the deadline expires, when the returned cancel function is called, or when the parent context's Done channel is closed, whichever happens first.
Canceling this context releases resources associated with it, so code should call cancel as soon as the operations running in this Context complete.
func WithDeadlineCause ¶
WithDeadlineCause behaves like WithDeadline but also sets the cause of the returned Context when the deadline is exceeded. The returned CancelFunc does not set the cause.
func WithTimeout ¶
func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc)
WithTimeout returns WithDeadline(parent, time.Now().Add(timeout)).
Canceling this context releases resources associated with it, so code should call cancel as soon as the operations running in this Context complete:
func slowOperationWithTimeout(ctx context.Context) (Result, error) { ctx, cancel := context.WithTimeout(ctx, 100*time.Millisecond) defer cancel() // releases resources if slowOperation completes before timeout elapses return slowOperation(ctx) }
func WithTimeoutCause ¶
WithTimeoutCause behaves like WithTimeout but also sets the cause of the returned Context when the timeout expires. The returned CancelFunc does not set the cause.
Types ¶
type CancelCauseFunc ¶
type CancelCauseFunc = context.CancelCauseFunc
A CancelCauseFunc behaves like a CancelFunc but additionally sets the cancellation cause. This cause can be retrieved by calling Cause on the canceled Context or on any of its derived Contexts.
If the context has already been canceled, CancelCauseFunc does not set the cause. For example, if childContext is derived from parentContext:
- if parentContext is canceled with cause1 before childContext is canceled with cause2, then Cause(parentContext) == Cause(childContext) == cause1
- if childContext is canceled with cause2 before parentContext is canceled with cause1, then Cause(parentContext) == cause1 and Cause(childContext) == cause2
type CancelFunc ¶
type CancelFunc = context.CancelFunc
A CancelFunc tells an operation to abandon its work. A CancelFunc does not wait for the work to stop. A CancelFunc may be called by multiple goroutines simultaneously. After the first call, subsequent calls to a CancelFunc do nothing.
type Context ¶
A Context carries a deadline, a cancellation signal, and other values across API boundaries.
Context's methods may be called by multiple goroutines simultaneously.
func Attach ¶
Attach attaches the audit, logger, and metrics clients to the context. This is generally not called directly, but is used by Background() and things like RPC packages that need to attach these to an already existing context.
func Background ¶
func Background() Context
Background returns a non-nil, empty Context. It is never canceled, and has no deadline. It is typically used by the main function, initialization, and tests, and as the top-level Context for incoming requests. This differs from the Background() function in the context package in that it attaches various clients to the context. This currently attaches:
- log.Default(), a *slog.Logger. - metrics.Default(), a metric.MeterProvider. - worker.Default(), a *worker.Pool. - background.Default(), a *background.Tasks.
These can be accessed using the Audit()/Log()/Metrics functions.
func TODO ¶
func TODO() Context
TODO returns a non-nil, empty Context. Code should use context.TODO when it's unclear which Context to use or it is not yet available (because the surrounding function has not yet been extended to accept a Context parameter).
func WithValue ¶
WithValue returns a copy of parent in which the value associated with key is val.
Use context Values only for request-scoped data that transits processes and APIs, not for passing optional parameters to functions.
The provided key must be comparable and should not be of type string or any other built-in type to avoid collisions between packages using context. Users of WithValue should define their own types for keys. To avoid allocating when assigning to an interface{}, context keys often have concrete type struct{}. Alternatively, exported context
func WithoutCancel ¶
WithoutCancel returns a copy of parent that is not canceled when parent is canceled. The returned context returns no Deadline or Err, and its Done channel is nil. Calling Cause on the returned context returns nil.