context

package
v0.1.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 16, 2024 License: MIT Imports: 3 Imported by: 6

Documentation

Overview

Package context provides the context functions

Package context provides the context functions

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AfterFunc

func AfterFunc(ctx Context, f func()) func() bool

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

func Cause(ctx Context) error

Cause returns the underlying cause of the context's cancellation or nil if it was not caused by another error.

func FromCreatedBy

func FromCreatedBy(ctx Context) string

FromCreatedBy retrieves the creator information from the given context.

It takes a Context as a parameter and returns a string.

func FromDB

func FromDB(ctx Context) (any, bool)

FromDB retrieves a db client from the context.

It takes a Context as a parameter and returns a db client.

func FromID

func FromID(ctx Context) string

FromID returns the user ID from the context.

It takes a Context as a parameter and returns a string.

func FromMapContext

func FromMapContext(parent Context) map[any]any

FromMapContext retrieves all values from the context.

func FromRowLock

func FromRowLock(ctx Context) bool

FromRowLock checks if the row is locked in the given context.

It takes a Context as a parameter and returns a boolean.

func FromSpan

func FromSpan(ctx Context) string

FromSpan returns the span id from the context.

It takes a Context as a parameter and returns a string.

func FromStack

func FromStack(ctx Context) string

FromStack retrieves the stack from the context.

It takes a Context as a parameter and returns a string.

func FromTag

func FromTag(ctx Context) string

FromTag retrieves the tag from the context.

It takes a Context as a parameter and returns a string.

func FromToken

func FromToken(ctx Context) string

FromToken returns the user token from the context.

It takes a Context as a parameter and returns a string.

func FromTrace

func FromTrace(ctx Context) string

FromTrace returns the trace id from the context.

It takes a Context as a parameter and returns a string.

func FromTrans

func FromTrans(ctx Context) (any, bool)

FromTrans retrieves a tx client from the context.

func FromUserCache

func FromUserCache(ctx Context) (any, bool)

FromUserCache returns the userCache from the context.

It takes a Context as a parameter and returns a userCache value.

func Value

func Value(ctx Context, key any) any

Value retrieves the value for the given key or nil if no value is present.

func WithCancel

func WithCancel(parent 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, 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

func WithDeadline(parent Context, deadline time.Time) (Context, CancelFunc)

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, 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

func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc)

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, 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.

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

type Context = context.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

func NewCreatedBy(ctx Context, by string) Context

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

func NewDB(ctx Context, db any) Context

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 NewID

func NewID(ctx Context, id string) Context

NewID returns a new context with the provided userID value.

It takes a context and a userID string as parameters and returns a context.

func NewRowLock

func NewRowLock(ctx Context) Context

NewRowLock creates a new context with a row lock value.

func NewSpan

func NewSpan(ctx Context, span string) Context

NewSpan creates a new context with the provided span value.

It takes a context and a span string as parameters and returns a context.

func NewStack

func NewStack(ctx Context, stack string) Context

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

func NewTag(ctx Context, tag string) Context

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 NewToken

func NewToken(ctx Context, token string) Context

NewToken returns a new context with the provided userToken value.

It takes a context and a userToken string as parameters and returns a context.

func NewTrace

func NewTrace(ctx Context, trace string) Context

NewTrace returns a new context with the provided trace value.

It takes a context and a trace string as parameters and returns a context.

func NewTrans

func NewTrans(ctx Context, tx any) Context

NewTrans creates a new context with the provided tx client value.

func NewUserCache

func NewUserCache(ctx Context, userCache any) Context

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 TODO

func TODO() Context

TODO returns an empty Context. It is never canceled, has no deadline, and has no values.

func WithContext

func WithContext(ctx Context) Context

WithContext returns a new context with the provided context.Context value.

func WithMapValue

func WithMapValue(parent Context, key, val any) Context

WithMapValue creates a new context with the provided key-value pair. If the context saved over than 500 keys, use WithMapValue instead.

func WithValue

func WithValue(parent Context, key, val any) Context

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

func WithoutCancel(parent Context) Context

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL