ctxutil

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2022 License: MIT Imports: 8 Imported by: 3

Documentation

Overview

Package ctxutil provides some helper functions which makes the context.Context object seemed to be an typed struct You don't need to know the keys just Set and Get

Attention: Before calling IncDBTime or IncRedisTime to accumulate the overall time you need call SetDBTime first.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotExist specifies that the data you want to retrive is missed
	ErrNotExist = errors.New("key is not existed in the context")
	// ErrDurationNotSetted reports that duration must be setted before you use it to do accumulation
	ErrDurationNotSetted = errors.New("duration must be setted at the beginning of the goroutine")
	//ErrWrongType ...
	ErrWrongType = errors.New("wrong type")
)

Functions

func Format

func Format(ctx context.Context) (result string, finalErr error)

Format formats the context with only builtin keys

func GetCaller

func GetCaller(ctx context.Context) string

GetCaller ...

func GetCookies

func GetCookies(ctx context.Context) ([]*http.Cookie, error)

func GetDBTime

func GetDBTime(ctx context.Context) (time.Duration, error)

GetDBTime returns the accumulated time for operating database

func GetDegrade

func GetDegrade(ctx context.Context) int

GetDegrade ...

func GetHTTPRequest

func GetHTTPRequest(ctx context.Context) (*http.Request, error)

GetHTTPRequest ...

func GetHintCode

func GetHintCode(ctx context.Context) (int64, error)

GetHintCode gets hintCode from context, if hintCode not exists, ErrNotExist will be returned

func GetHintContent

func GetHintContent(ctx context.Context) (string, error)

GetHintContent gets hintContent from context, if hintContent not exists, ErrNotExist will be returned

func GetLang

func GetLang(ctx context.Context) (string, error)

GetLang gets language from context if langguage not exists, ErrNotExist will be returned

func GetRedisTime

func GetRedisTime(ctx context.Context) (time.Duration, error)

GetRedisTime returns the accumulated time for operating redis

func GetRequestInTs

func GetRequestInTs(ctx context.Context) (int64, error)

GetRequestInTs 获取请求的时间戳 毫秒位

func GetRequestTimeout

func GetRequestTimeout(ctx context.Context) (int64, error)

GetRequestTimeout 上游超时要求 毫秒位

func GetSpanID

func GetSpanID(ctx context.Context) (string, error)

GetSpanID gets spanID from context, if spanID not exists, ErrNotExist will be returned

func GetToken

func GetToken(ctx context.Context) (string, error)

GetToken ...

func GetTraceID

func GetTraceID(ctx context.Context) (string, error)

GetTraceID gets traceID from context, if traceID not exists, ErrNotExist will be returned

func IncDBTime

func IncDBTime(ctx context.Context, delta time.Duration) (context.Context, error)

IncDBTime accumulates the time for operating database you must call SetDBTime before use this function

func IncRedisTime

func IncRedisTime(ctx context.Context, delta time.Duration) (context.Context, error)

IncRedisTime accumulates the time for operating redis you must call SetRedisTime before use this function

func SetCaller

func SetCaller(ctx context.Context, caller string) context.Context

SetCaller ...

func SetCookies

func SetCookies(ctx context.Context, cks []*http.Cookie) context.Context

func SetDBTime

func SetDBTime(ctx context.Context) context.Context

func SetDegrade

func SetDegrade(ctx context.Context, sed int) context.Context

SetDegrade ...

func SetHTTPRequest

func SetHTTPRequest(ctx context.Context, r *http.Request) context.Context

SetHTTPRequest ...

func SetHintCode

func SetHintCode(ctx context.Context, hintCode int64) context.Context

SetHintCode sets the hintCode into context

func SetHintContent

func SetHintContent(ctx context.Context, hintContent string) context.Context

SetHintContent sets hintContent into context

func SetLang

func SetLang(ctx context.Context, language string) context.Context

SetLang sets language params into context

func SetRedisTime

func SetRedisTime(ctx context.Context) context.Context

SetRedisTime initialize the time counter for redis

func SetRequestInTs

func SetRequestInTs(ctx context.Context, ts int64) context.Context

SetRequestInTs 请求的时间戳 毫秒位

func SetRequestTimeout

func SetRequestTimeout(ctx context.Context, to int64) context.Context

SetRequestTimeout 上游超时要求 毫秒位

func SetSpanID

func SetSpanID(ctx context.Context, spanID string) context.Context

SetSpanID sets spanID into context

func SetToken

func SetToken(ctx context.Context, token string) context.Context

SetToken sets token into context

func SetTraceID

func SetTraceID(ctx context.Context, traceID string) context.Context

SetTraceID sets traceID into context

func TraceString

func TraceString(ctx context.Context) string

TraceString is the same as Format,but only return string that is, if there's an error occurs, TraceString will return "ctx_format=unset"

Types

This section is empty.

Jump to

Keyboard shortcuts

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