Documentation ¶
Overview ¶
Package context provides the context functions
Index ¶
- func AfterFunc(ctx Context, f func()) func() bool
- func Cause(ctx Context) error
- func FromCreatedBy(ctx Context) string
- func FromDB(ctx Context) (any, bool)
- func FromIsRootUser(ctx Context) bool
- func FromLogger(ctx Context) (*slog.Logger, bool)
- func FromRowLock(ctx Context) bool
- func FromStack(ctx Context) string
- func FromTag(ctx Context) string
- func FromTraceID(ctx Context) string
- func FromTrans(ctx Context) (any, bool)
- func FromUserCache(ctx Context) (any, bool)
- func FromUserID(ctx Context) string
- func FromUserToken(ctx Context) string
- type CancelCauseFunc
- type CancelFunc
- type Context
- func Background() Context
- func NewCreatedBy(ctx Context, by string) Context
- func NewDB(ctx Context, db any) Context
- func NewIsRootUser(ctx Context) Context
- func NewLogger(ctx Context, logger *slog.Logger) Context
- func NewRowLock(ctx Context) Context
- func NewStack(ctx Context, stack string) Context
- func NewTag(ctx Context, tag string) Context
- func NewTraceID(ctx Context, traceID string) Context
- func NewTrans(ctx Context, db any) Context
- func NewUserCache(ctx Context, userCache any) Context
- func NewUserID(ctx Context, userID string) Context
- func NewUserToken(ctx Context, userToken string) Context
- func TODO() Context
- func WithCancel(parent Context) (Context, context.CancelFunc)
- func WithCancelCause(parent Context) (Context, context.CancelCauseFunc)
- func WithDeadline(parent Context, deadline time.Time) (Context, context.CancelFunc)
- func WithDeadlineCause(parent Context, deadline time.Time, cause error) (Context, context.CancelFunc)
- func WithTimeout(parent Context, timeout time.Duration) (Context, context.CancelFunc)
- func WithTimeoutCause(parent Context, timeout time.Duration, cause error) (Context, context.CancelFunc)
- func WithValue(parent Context, key, val any) Context
- func WithoutCancel(parent Context) Context
- type Interface
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AfterFunc ¶
AfterFunc waits for the duration to elapse and then calls f in its own goroutine. If the context is canceled before the duration elapses, f will not be called.
func Cause ¶
Cause returns the underlying cause of the context's cancellation or nil if it was not caused by another error.
func FromCreatedBy ¶
FromCreatedBy retrieves the creator information from the given context.
It takes a Context as a parameter and returns a string.
func FromDB ¶
FromDB retrieves a db client from the context.
It takes a Context as a parameter and returns a db client.
func FromIsRootUser ¶
FromIsRootUser returns the isRootUser from the context.
It takes a Context as a parameter and returns a boolean.
func FromLogger ¶
FromLogger retrieves a logger from the context.
It takes a Context as a parameter and returns a logger.
func FromRowLock ¶
FromRowLock checks if the row is locked in the given context.
It takes a Context as a parameter and returns a boolean.
func FromStack ¶
FromStack retrieves the stack from the context.
It takes a Context as a parameter and returns a string.
func FromTag ¶
FromTag retrieves the tag from the context.
It takes a Context as a parameter and returns a string.
func FromTraceID ¶
FromTraceID returns the trace ID from the context.
It takes a Context as a parameter and returns a string.
func FromUserCache ¶
FromUserCache returns the userCache from the context.
It takes a Context as a parameter and returns a userCache value.
func FromUserID ¶
FromUserID returns the user ID from the context.
It takes a Context as a parameter and returns a string.
func FromUserToken ¶
FromUserToken returns the user token from the context.
It takes a Context as a parameter and returns a string.
Types ¶
type CancelCauseFunc ¶
type CancelCauseFunc = context.CancelCauseFunc
All context types are defined in the context package.
type CancelFunc ¶
type CancelFunc = context.CancelFunc
All context types are defined in the context package.
type Context ¶
All context types are defined in the context package.
func Background ¶
func Background() Context
Background returns a non-nil, empty Context. It is never canceled, has no deadline, and has no values. Background is typically used by the main function, initialization, and tests, and as the top-level Context for incoming requests.
func NewCreatedBy ¶
NewCreatedBy creates a new context with the provided 'by' value
It takes a Context and a 'by' string as parameters and returns a context.
func NewDB ¶
NewDB creates a new context with the provided db client value.
It takes a context and a db client as parameters and returns a context.
func NewIsRootUser ¶
NewIsRootUser returns a new context with the provided isRootUser value.
It takes a Context as a parameter and returns a context.
func NewLogger ¶
NewLogger creates a new context with the provided logger value.
It takes a Context and a logger as parameters and returns a context.
func NewRowLock ¶
NewRowLock creates a new context with a row lock value.
func NewStack ¶
NewStack creates a new context with the provided stack value.
It takes a Context and a stack string as parameters and returns a context.
func NewTag ¶
NewTag creates a new context with the provided tag value.
It takes a Context and a tag string as parameters and returns a context.
func NewTraceID ¶
NewTraceID returns a new context with the provided traceID value.
It takes a context and a traceID string as parameters and returns a context.
func NewUserCache ¶
NewUserCache returns a new context with the provided userCache value.
It takes a Context and a userCache value as parameters and returns a context.
func NewUserID ¶
NewUserID returns a new context with the provided userID value.
It takes a context and a userID string as parameters and returns a context.
func NewUserToken ¶
NewUserToken returns a new context with the provided userToken value.
It takes a context and a userToken string as parameters and returns a context.
func TODO ¶
func TODO() Context
TODO returns an empty Context. It is never canceled, has no deadline, and has no values.
func WithCancel ¶
func WithCancel(parent Context) (Context, context.CancelFunc)
WithCancel returns a copy of the parent Context 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.
func WithCancelCause ¶
func WithCancelCause(parent Context) (Context, context.CancelCauseFunc)
WithCancelCause returns a copy of the parent Context with a new Done channel and an associated cause. The returned context's Done channel is closed when the returned cancel function is called, when the parent context's Done channel is closed, or when the provided cause is returned by the parent context's Err method, whichever happens first.
func WithDeadline ¶
WithDeadline returns a copy of the parent Context with a 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's 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.
func WithDeadlineCause ¶
func WithDeadlineCause(parent Context, deadline time.Time, cause error) (Context, context.CancelFunc)
WithDeadlineCause returns a copy of the parent Context with a deadline adjusted to be no later than d and an associated cause. If the parent's deadline is already earlier than d, WithDeadlineCause(parent, d) is semantically equivalent to parent. The returned context's Done channel is closed when the deadline expires, when the returned cancel function is called, or when the provided cause is returned by the parent context's Err method, whichever happens first.
func WithTimeout ¶
WithTimeout returns a copy of the parent Context with a deadline adjusted to be no later than when the duration d elapses. It is equivalent to WithDeadline(parent, time.Now().Add(d)). The returned context's Done channel is closed when the timeout elapses, when the returned cancel function is called, or when the parent context's Done channel is closed, whichever happens first.
func WithTimeoutCause ¶
func WithTimeoutCause(parent Context, timeout time.Duration, cause error) ( Context, context.CancelFunc, )
WithTimeoutCause returns a copy of the parent Context with a deadline adjusted to be no later than when the duration d elapses and an associated cause. It is equivalent to WithDeadline(parent, time.Now().Add(d)). The returned context's Done channel is closed when the timeout elapses, when the returned cancel function is called, or when the provided cause is returned by the parent context's Err method, whichever happens first.
func WithValue ¶
WithValue returns a copy of the parent Context that carries the given key-value pair. The value's type must be comparable for equality; otherwise, WithValue will panic. Use context Values only for request-scoped data that transits processes and API boundaries, not for passing optional parameters to functions.
func WithoutCancel ¶
WithoutCancel returns a non-cancellable Context derived from parent. It is never canceled, has no deadline, and has the same values as the parent. If the parent is already non-cancellable, WithoutCancel returns the parent.
type Interface ¶
type Interface interface { Background() Context TODO() Context Canceled() error DeadlineExceeded() error WithCancel(ctx Context) (Context, context.CancelFunc) WithCancelCause(parent Context) (ctx Context, cancel CancelCauseFunc) Cause(ctx Context) error AfterFunc(ctx Context, f func()) (stop func() bool) WithoutCancel(ctx Context) Context WithDeadline(ctx Context, t time.Time) (Context, context.CancelFunc) WithDeadlineCause(ctx Context, t time.Time, e error) (Context, context.CancelFunc) WithTimeout(ctx Context, t time.Duration) (Context, context.CancelFunc) WithTimeoutCause(ctx Context, t time.Duration, e error) (Context, context.CancelFunc) WithValue(ctx Context, key any, val any) Context }