Documentation
¶
Index ¶
- Constants
- Variables
- func FastValue(ctx context.Context, key FastValueKey) any
- func LogTagsFromContext(ctx context.Context) *logtags.Buffer
- func WhenDone(parent context.Context, done WhenDoneFunc) bool
- func WithFastValue(parent context.Context, key FastValueKey, val any) context.Context
- func WithLogTags(ctx context.Context, tags *logtags.Buffer) context.Context
- type FastValueKey
- type FastValuesBuilder
- type WhenDoneFunc
Constants ¶
const MaxFastValues = 8
MaxFastValues is the number of FastValueKeys that can be registered.
To register new values, this constant will need to be increased (which could cause a regression in performance).
Variables ¶
var LogTagsKey = RegisterFastValueKey()
Functions ¶
func FastValue ¶
func FastValue(ctx context.Context, key FastValueKey) any
FastValue retrieves the value for the given key.
func LogTagsFromContext ¶
LogTagsFromContext returns the log tags from the context, if any.
func WhenDone ¶
func WhenDone(parent context.Context, done WhenDoneFunc) bool
WhenDone arranges for the specified function to be invoked when parent context becomes done and returns true. See context_bazel.go for the full documentation on this function. This version does the same but is missing an assertion that requires the patched Go runtime to work properly.
func WithFastValue ¶
WithFastValue creates a new context with a new value for the given key. It overrides any existing value for that same key.
The given value will be returned by FastValue(ctx, key). The key must have been generated using RegisterFastValueKey().
This is a more efficient alternative to using context.WithValue() and Context.Value().
Types ¶
type FastValueKey ¶
type FastValueKey uint8
FastValueKey is a key that can be used to get and set a fast value. Keys must be initialized using RegisterFastValueKey().
func RegisterFastValueKey ¶
func RegisterFastValueKey() FastValueKey
RegisterFastValueKey creates a key that can be used with WithFastValue(). This is intended to be called from global initialization code.
Only MaxFastValues calls to RegisterFastValueKey are allowed for the lifetime of the binary; only a handful of very frequent in-context values should use this infrastructure.
type FastValuesBuilder ¶
type FastValuesBuilder struct {
// contains filtered or unexported fields
}
FastValuesBuilder contains multiple values; used for WithFastValues.
func WithFastValues ¶
func WithFastValues(parent context.Context) FastValuesBuilder
WithFastValues starts the process of creating a new context that sets a number of fast values. The returned builder can be used to set values and construct the context; it also provides efficient access to the current values in the parent context.
Sample usage:
b := WithFastValues(ctx) b.Set(key1, val1) b.Set(key2, val2) ctx = b.Finish()
func WithFastValuesPrealloc ¶
func WithFastValuesPrealloc(parent context.Context) FastValuesBuilder
WithFastValuesPrealloc is like WithFastValues, but preallocates multiple contexts that can later be reused on subsequent WithFastValue(s) calls to save on allocations.
func (*FastValuesBuilder) Finish ¶
func (b *FastValuesBuilder) Finish() context.Context
Finish constructs the context with the values set by Set().
The FastValuesBuilder must not be used again.
func (*FastValuesBuilder) Get ¶
func (b *FastValuesBuilder) Get(key FastValueKey) any
Get gets the value for the key in the context being built. If this key was set before, it returns the last value passed to Set(); otherwise it returns the value in the context that was passed to WithFastValues().
func (*FastValuesBuilder) Set ¶
func (b *FastValuesBuilder) Set(key FastValueKey, val any)
Set sets the value for the key in the context being built.
type WhenDoneFunc ¶
type WhenDoneFunc func()
WhenDoneFunc is the callback invoked by context when it becomes done.