goid

package
v1.3.5 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2022 License: Apache-2.0 Imports: 10 Imported by: 2

README

goid

goid包是go版的ThreadLocal,用于在多个协程之间传递数据

// 变量分配
var TestIdLocal = goid.NewLocalStorage()

// 设置
TestIdLocal.Set(tenantId)

// 获取
TestIdLocal.Get()

注意:
在遇到协程的时候,请不要使用go的原生方式,请使用如下的方式,否则goid数据无法传递

// 启用协程
goid.Go(func() { 
    // ...
})

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllGoids

func AllGoids() (ids []int64)

AllGoids return all goroutine's goid in the current golang process. It will try load all goid from runtime natively for better performance, and fallover to runtime.Stack, which is realy inefficient.

func GenerateTraceID added in v0.9.0

func GenerateTraceID() string

func GenerateUUID added in v0.9.5

func GenerateUUID() string

func Go

func Go(f func())

Go start an new goroutine, and copy all local storages from current goroutine.

func Goid

func Goid() (id int64)

Goid return the current goroutine's unique id. It will try get gid by native cgo/asm for better performance, and could parse gid from stack for failover supporting.

func InheritContext

func InheritContext(ic *ImmutableContext)

InheritContext load the specified ImmutableContext instance into the local storage of current goroutine.

func NativeGoid added in v1.2.0

func NativeGoid() int64

Types

type ImmutableContext

type ImmutableContext struct {
	// contains filtered or unexported fields
}

ImmutableContext represents all local storages of one goroutine.

func BackupContext

func BackupContext() *ImmutableContext

BackupContext copy all local storages into an ImmutableContext instance.

type LocalStorage

type LocalStorage interface {

	// Get returns the value in the current goroutine's local storage, if it was set before.
	Get() (value any)

	// Set copy the value into the current goroutine's local storage, and return the old value.
	Set(value any) (oldValue any)

	// Del delete the value from the current goroutine's local storage, and return it.
	Del() (oldValue any)

	// Clear delete values from all goroutine's local storages.
	Clear()
}

LocalStorage provides goroutine-local variables.

func NewLocalStorage

func NewLocalStorage() LocalStorage

NewLocalStorage create and return an new LocalStorage instance.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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