context

package
v0.0.30 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

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 FromIsRootUser

func FromIsRootUser(ctx Context) bool

FromIsRootUser returns the isRootUser from the context.

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

func FromLogger

func FromLogger(ctx Context) (*slog.Logger, bool)

FromLogger retrieves a logger from the context.

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

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

func FromTraceID(ctx Context) string

FromTraceID 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 dbx 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 FromUserID

func FromUserID(ctx Context) string

FromUserID returns the user ID from the context.

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

func FromUserToken

func FromUserToken(ctx Context) string

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

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 NewIsRootUser

func NewIsRootUser(ctx Context) Context

NewIsRootUser returns a new context with the provided isRootUser value.

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

func NewLogger

func NewLogger(ctx Context, logger *slog.Logger) Context

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

func NewRowLock(ctx Context) Context

NewRowLock creates a new context with a row lock value.

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 NewTraceID

func NewTraceID(ctx Context, traceID string) Context

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 NewTrans

func NewTrans(ctx Context, db any) Context

NewTrans creates a new context with the provided dbx 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 NewUserID

func NewUserID(ctx Context, userID string) Context

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

func NewUserToken(ctx Context, userToken string) Context

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

func WithDeadline(parent Context, deadline time.Time) (Context, 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, 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, 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, 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

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.

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
}

func Alias

func Alias() Interface

Alias returns an Interface implementation that provides access to the context methods.

Jump to

Keyboard shortcuts

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