gls

package
v7.0.4+incompatible Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2020 License: MIT, MIT Imports: 3 Imported by: 0

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

func GenSym

func GenSym() ContextKey

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

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.

type Values

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.

Jump to

Keyboard shortcuts

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