Repository navigation
util/ctxkey: add package for type-safe context keys - #10841
Conversation
|
General implementation question: IIRC, context values are implemented as a linked list within the stdlib context package, so lookups get increasingly slow as more keys get piled into the context. Given the external API of this library, it seems to me like we could use a package-internal secret key to register a map[*string]any onto the context, and then attach all externally defined keys to that map. That would keep the context chain constant length as we add more data over time. And given that you don't give callers access to the raw key already, this change would be invisible to package users. Would that be worth doing, or do we expect the context linked list to remain short enough that it's not worth it? |
|
Interesting idea, but I don't know how we would prevent the mutability of the map from leaking. Consider the following: ctx1 := context.Background()
ctx2 := key.WithValue(ctx1, "foo") // presumably this creates the underlying map
ctx2a := key.WithValue(ctx2, "bar") // this mutates the map
ctx2b := key.WithValue(ctx2, "baz") // this also mutates the map
fmt.Println(key.Value(ctx2)) // should print foo
fmt.Println(key.Value(ctx2a)) // should print bar
fmt.Println(key.Value(ctx2b)) // should print bazwith the map idea, all of them would print "baz", which is incorrect. |
d0145e9 to
19d9d53
Compare
|
Adjusted the code to assist in debugging. Printing a context will now print the key and values in the entire chain after https://go-review.googlesource.com/c/go/+/555697 gets merged upstream. |
742610f to
b88d487
Compare
The lack of type-safety in context.WithValue leads to the common pattern
of defining of package-scoped type to ensure global uniqueness:
type fooKey struct{}
func withFoo(ctx context, v Foo) context.Context {
return context.WithValue(ctx, fooKey{}, v)
}
func fooValue(ctx context) Foo {
v, _ := ctx.Value(fooKey{}).(Foo)
return v
}
where usage becomes:
ctx = withFoo(ctx, foo)
foo := fooValue(ctx)
With many different context keys, this can be quite tedious.
Using generics, we can simplify this as:
var fooKey = ctxkey.New("mypkg.fooKey", Foo{})
where usage becomes:
ctx = fooKey.WithValue(ctx, foo)
foo := fooKey.Value(ctx)
See https://go.dev/issue/49189
Updates #cleanup
Signed-off-by: Joe Tsai <[email protected]>
The lack of type-safety in context.WithValue leads to the common pattern
of defining of package-scoped type to ensure global uniqueness:
type fooKey struct{}
func withFoo(ctx context, v Foo) context.Context {
return context.WithValue(ctx, fooKey{}, v)
}
func fooValue(ctx context) Foo {
v, _ := ctx.Value(fooKey{}).(Foo)
return v
}
where usage becomes:
ctx = withFoo(ctx, foo)
foo := fooValue(ctx)
With many different context keys, this can be quite tedious.
Using generics, we can simplify this as:
var fooKey = ctxkey.New("mypkg.fooKey", Foo{})
where usage becomes:
ctx = fooKey.WithValue(ctx, foo)
foo := fooKey.Value(ctx)
See https://go.dev/issue/49189
Updates #cleanup
Signed-off-by: Joe Tsai <[email protected]>
The lack of type-safety in context.WithValue leads to the common pattern
of defining of package-scoped type to ensure global uniqueness:
type fooKey struct{}
func withFoo(ctx context, v Foo) context.Context {
return context.WithValue(ctx, fooKey{}, v)
}
func fooValue(ctx context) Foo {
v, _ := ctx.Value(fooKey{}).(Foo)
return v
}
where usage becomes:
ctx = withFoo(ctx, foo)
foo := fooValue(ctx)
With many different context keys, this can be quite tedious.
Using generics, we can simplify this as:
var fooKey = ctxkey.New("mypkg.fooKey", Foo{})
where usage becomes:
ctx = fooKey.WithValue(ctx, foo)
foo := fooKey.Value(ctx)
See https://go.dev/issue/49189
Updates #cleanup
Signed-off-by: Joe Tsai <[email protected]>
The lack of type-safety in context.WithValue leads to the common pattern of defining of package-scoped type to ensure global uniqueness:
where usage becomes:
With many different context keys, this can be quite tedious.
Using generics, we can simplify this as:
where usage becomes:
See https://go.dev/issue/49189
Updates #cleanup