Documentation ¶
Overview ¶
Package gls implements goroutine-local storage.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Go ¶
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.
Example ¶
var ( mgr = NewContextManager() request_id_key = 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(Values{request_id_key: "12345"}, func() { var wg sync.WaitGroup wg.Add(1) go func() { defer wg.Done() MyLog() }() wg.Wait() wg.Add(1) Go(func() { defer wg.Done() MyLog() }) wg.Wait() })
Output: No request id found My request id is: 12345
Types ¶
type ContextKey ¶
type ContextKey struct {
// contains filtered or unexported fields
}
ContextKey is a throwaway value you can use as a key to a ContextManager
type ContextManager ¶
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 ¶
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 ¶
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 ¶
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.
Example ¶
var ( mgr = NewContextManager() request_id_key = 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(Values{request_id_key: "12345"}, func() { MyLog() }) MyLog()
Output: My request id is: 12345 No request id found
func (*ContextManager) Unregister ¶
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.