Skip to content

all: make use of ctxkey everywhere - #10846

Merged
dsnet merged 1 commit into
mainfrom
dsnet/ctxkey-use
Jan 16, 2024
Merged

dsnet merged 1 commit into
mainfrom
dsnet/ctxkey-use

Conversation

@dsnet

@dsnet dsnet commented Jan 13, 2024 •

Copy link
Copy Markdown
Member

Also perform minor cleanups on the ctxkey package itself.
Provide guidance on when to use ctxkey.Key[T] over ctxkey.New.
Also, allow for interface kinds because the value wrapping trick
also happens to fix edge cases with interfaces in Go.

Updates #cleanup

@dsnet
dsnet force-pushed the dsnet/ctxkey-use branch 12 times, most recently from d225318 to 3babb83 Compare January 13, 2024 19:43
Also perform minor cleanups on the ctxkey package itself.
Provide guidance on when to use ctxkey.Key[T] over ctxkey.New.
Also, allow for interface kinds because the value wrapping trick
also happens to fix edge cases with interfaces in Go.

Updates #cleanup

Signed-off-by: Joe Tsai <[email protected]>
Comment thread types/logger/logger.go

// FromContext extracts a log function from ctx.
//
// Deprecated: Use [LogfKey.Value] instead.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there some reason not to have a semantically-relevant helper? It's not clear to me we get much benefit from moving the thing the caller depends on from a simple helper function to a compound call on a value in the package. (I get that the helper is no longer necessary, but I think it reads better, esp. given that loggers are going to be used ~everywhere. But YMMV).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One detriment of helpers is pollution of the namespace.

With ctxkey, we end up with single declaration like:

var ThingKey ctxkey[T] = ...

In contrast, the helpers come as a pair of declarations typically named as:

func WithThing(context.Context, T) context.Context
func ThingValue(context.Context) (T, bool)

From godoc, these pair of helpers don't sort next to each other, making detection more difficult. Trying to find helpers that deal with context values is somewhat difficult as searching for "context.Context" or "T" usually produces many false positives. On the other hand, we can just search for "ctxkey".

The other detriment is the need to write extra code. One of the value propositions of ctxkey.Key is that you don't have manually declare these two helpers to gain type safety. If we're always going to declare the helper functions in addition to using ctxkey.Key, then I'm not sure if ctxkey.Key is providing any value.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a blocker, but I disagree with the weighting of that reasoning: Not that layout doesn't matter, but cognitive burden to the reader at the call site is IMO more important than, say, where the functions sort in a listing.

And we're still "polluting" the namespace, only now it's with a mutable variable (morally immutable, but as a reader when I see a variable reference I know it's a variable, and that makes me attend to whether that affects its use).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the callsite, we have:

construction extraction
with ctxkey ctx := mypkg.ThingKey.WithValue(ctx, v) v := mypkg.ThingKey.Value(ctx)
user defined ctx := mypkg.WithThing(ctx, v) v := mypkg.ThingValue(ctx)

Thoughts:

  • The existence of the ThingKey namespace could be seen as additional cognitive burden.
  • However, with ctxkey, we will always have a WithValue(ctx, ...) or Value(ctx) suffix, while with user-defined helpers, we're left with whether people follow a consistent convention (and we don't). I'd argue that consistency helps with cognitive burden.
  • Using ctxkey is not always more typing as some helpers are named as ContextWithThing or ThingFromContext, which can be longer.

it's a variable, and that makes me attend to whether that affects its use

Fair criticism. It'd be nice if I could make it a constant, but I don't know how. The alternative is to make it a argument-less function that just returns the ctxkey.Key, but that seems to buy us little.

@dsnet
dsnet merged commit c25968e into main Jan 16, 2024
@dsnet
dsnet deleted the dsnet/ctxkey-use branch January 16, 2024 21:56
Asutorufa pushed a commit to Asutorufa/tailscale that referenced this pull request Jan 19, 2024
Also perform minor cleanups on the ctxkey package itself.
Provide guidance on when to use ctxkey.Key[T] over ctxkey.New.
Also, allow for interface kinds because the value wrapping trick
also happens to fix edge cases with interfaces in Go.

Updates #cleanup

Signed-off-by: Joe Tsai <[email protected]>
chen8945 pushed a commit to Ckid-Home/tailscale that referenced this pull request Jul 31, 2024
Also perform minor cleanups on the ctxkey package itself.
Provide guidance on when to use ctxkey.Key[T] over ctxkey.New.
Also, allow for interface kinds because the value wrapping trick
also happens to fix edge cases with interfaces in Go.

Updates #cleanup

Signed-off-by: Joe Tsai <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants