Documentation ¶
Overview ¶
Package gls implements goroutine-local storage.
Index ¶
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.
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.
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.