gls

package
v1.3.17 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2023 License: Apache-2.0, Apache-2.0 Imports: 3 Imported by: 0

README

goroutine local storage

Sourcegraph GoDoc Build Status codecov rcard License

Thanks https://github.com/huandu/go-tls for original idea

  • get current goroutine id
  • goroutine local storage

require go version >= 1.4

gls.GoID

get the identifier unique for this goroutine

go func() {
	gls.GoID()
}()
go func() {
	gls.GoID()
}()

gls.Set / gls.Get

goroutine local storage is a map[interface{}]interface{} local to current goroutine

It is intended to be used by framworks to simplify context passing.

Use context.Context to pass context if possible.

gls.Set("user_id", "abc")
doSomeThing()

func doSomeThing() {
	gls.Get("user_id") // will be "abc"
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeleteGls

func DeleteGls(goid int64)

DeleteGls remove goroutine local storage for specified goroutine

func Get

func Get(key interface{}) interface{}

Get key from goroutine local storage

func GetGls

func GetGls(goid int64) map[interface{}]interface{}

GetGls get goroutine local storage for specified goroutine if the goroutine did not set gls, it will return nil

func GoID

func GoID() int64

GoID returns the goroutine id of current goroutine

func IsGlsEnabled

func IsGlsEnabled(goid int64) bool

IsGlsEnabled test if the gls is available for specified goroutine

func Remove

func Remove(key interface{})

func ResetGls

func ResetGls(goid int64, initialValue map[interface{}]interface{})

ResetGls reset the goroutine local storage for specified goroutine

func Set

func Set(key interface{}, value interface{})

Set key and element to goroutine local storage

func WithEmptyGls

func WithEmptyGls(f func()) func()

WithEmptyGls works like WithGls, but do not inherit gls from parent goroutine.

func WithGls

func WithGls(f func()) func()

WithGls setup and teardown the gls in the wrapper. go WithGls(func(){}) will add gls for the new goroutine. The gls will be removed once goroutine exit

Types

This section is empty.

Jump to

Keyboard shortcuts

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