gls: github.com/jtolds/gls Index | Examples | Files

package gls

import "github.com/jtolds/gls"

Package gls implements goroutine-local storage.

Index

Examples

Package Files

context.go gen_sym.go gid.go id_pool.go stack_tags.go stack_tags_main.go

func EnsureGoroutineId Uses

func EnsureGoroutineId(cb func(gid uint))

Will call cb with the current goroutine identifier. If one hasn't already been generated, one will be created and set first. The goroutine identifier might be invalid after cb returns.

func GetGoroutineId Uses

func GetGoroutineId() (gid uint, ok bool)

Will return this goroutine's identifier if set. If you always need a goroutine identifier, you should use EnsureGoroutineId which will make one if there isn't one already.

func Go Uses

func Go(cb func())

Go preserves ContextManager values and Goroutine-local-storage across new goroutine invocations. The Go method makes a copy of all existing values on all registered context managers and makes sure they are still set after kicking off the provided function in a new goroutine. If you don't use this Go method instead of the standard 'go' keyword, you will lose values in ContextManagers, as goroutines have brand new stacks.

Code:

var (
    mgr            = gls.NewContextManager()
    request_id_key = gls.GenSym()
)

MyLog := func() {
    if request_id, ok := mgr.GetValue(request_id_key); ok {
        fmt.Println("My request id is:", request_id)
    } else {
        fmt.Println("No request id found")
    }
}

mgr.SetValues(gls.Values{request_id_key: "12345"}, func() {
    var wg sync.WaitGroup
    wg.Add(1)
    go func() {
        defer wg.Done()
        MyLog()
    }()
    wg.Wait()
    wg.Add(1)
    gls.Go(func() {
        defer wg.Done()
        MyLog()
    })
    wg.Wait()
})

Output:

No request id found
My request id is: 12345

type ContextKey Uses

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

ContextKey is a throwaway value you can use as a key to a ContextManager

func GenSym Uses

func GenSym() ContextKey

GenSym will return a brand new, never-before-used ContextKey

type ContextManager Uses

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

ContextManager is the main entrypoint for interacting with Goroutine-local-storage. You can have multiple independent ContextManagers at any given time. ContextManagers are usually declared globally for a given class of context variables. You should use NewContextManager for construction.

func NewContextManager Uses

func NewContextManager() *ContextManager

NewContextManager returns a brand new ContextManager. It also registers the new ContextManager in the ContextManager registry which is used by the Go method. ContextManagers are typically defined globally at package scope.

func (*ContextManager) GetValue Uses

func (m *ContextManager) GetValue(key interface{}) (
    value interface{}, ok bool)

GetValue will return a previously set value, provided that the value was set by SetValues somewhere higher up the stack. If the value is not found, ok will be false.

func (*ContextManager) SetValues Uses

func (m *ContextManager) SetValues(new_values Values, context_call func())

SetValues takes a collection of values and a function to call for those values to be set in. Anything further down the stack will have the set values available through GetValue. SetValues will add new values or replace existing values of the same key and will not mutate or change values for previous stack frames. SetValues is slow (makes a copy of all current and new values for the new gls-context) in order to reduce the amount of lookups GetValue requires.

Code:

var (
    mgr            = gls.NewContextManager()
    request_id_key = gls.GenSym()
)

MyLog := func() {
    if request_id, ok := mgr.GetValue(request_id_key); ok {
        fmt.Println("My request id is:", request_id)
    } else {
        fmt.Println("No request id found")
    }
}

mgr.SetValues(gls.Values{request_id_key: "12345"}, func() {
    MyLog()
})
MyLog()

Output:

My request id is: 12345
No request id found

func (*ContextManager) Unregister Uses

func (m *ContextManager) Unregister()

Unregister removes a ContextManager from the global registry, used by the Go method. Only intended for use when you're completely done with a ContextManager. Use of Unregister at all is rare.

type Values Uses

type Values map[interface{}]interface{}

Values is simply a map of key types to value types. Used by SetValues to set multiple values at once.

Package gls imports 2 packages (graph) and is imported by 49 packages. Updated 2017-09-17. Refresh now. Tools for package owners.